Skip to content

Instantly share code, notes, and snippets.

@ivanbrennan
Created June 24, 2018 14:22
Show Gist options
  • Save ivanbrennan/27b0df722f6ad7f7d74e07fbf1e404bc to your computer and use it in GitHub Desktop.
Save ivanbrennan/27b0df722f6ad7f7d74e07fbf1e404bc to your computer and use it in GitHub Desktop.
# install MySQL Server
nix-env --install --attr nixos.mysql57
sudo su
groupadd mysql
useradd -M \
--home-dir /var/lib/mysql \
--no-user-group \
--system \
--gid mysql \
--shell /bin/false \
--comment 'MySQL Server' \
mysql
cat > /etc/my.cnf <<EOF
[mysqld]
datadir=/var/lib/mysql
socket=/run/mysqld/mysqld.sock
port=3306
log-error=/var/log/mysqld.err
user=mysql
EOF
chown root:root /etc/my.cnf
chmod 644 /etc/my.cnf
cd /var/lib/
mkdir mysql
chmod 750 mysql
chown mysql:mysql mysql
cd /var/log
touch mysqld.err
chown mysql:mysql mysqld.err
chmod 0640 mysqld.err
cd /run
mkdir mysqld
chmod 755 mysqld
chown mysql:mysql mysqld
/home/ivan/.nix-profile/bin/mysqld \
--defaults-file=/etc/my.cnf --initialize
cat > /etc/systemd/system/mysqld.service <<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(7)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
Type=forking
PIDFile=/var/lib/mysql/mysqld.pid
# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0
# Start main service
ExecStart=/home/ivan/.nix-profile/bin/mysqld --defaults-file=/etc/my.cnf --daemonize --pid-file=/var/lib/mysql/mysqld.pid $MYSQLD_OPTS
# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql
# Sets open_files_limit
LimitNOFILE = 5000
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=false
EOF
chmod 664 /etc/systemd/system/mysqld.service
cat > /etc/tmpfiles.d/mysql.conf <<EOF
d /var/lib/mysql 0750 mysql mysql -
EOF
chmod 644 /etc/tmpfiles.d/mysql.conf
systemctl start mysqld
systemctl status mysqld
journalctl -u mysqld
awk '/A temporary password is generated for root@localhost/{ print $NF }' \
/var/log/mysqld.err
/home/ivan/.nix-profile/bin/mysql -u root -p
# > ALTER USER 'root'@'localhost' IDENTIFIED BY 'insert-new-password-here';
exit
mysqlshow -u root -p
mysqladmin -u root -p version
# Useful references:
# https://dev.mysql.com/doc/mysql-secure-deployment-guide/5.7/en/
# https://dev.mysql.com/doc/refman/5.7/en/binary-installation.html
# https://dev.mysql.com/doc/refman/5.7/en/using-systemd.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment