What's the proper way to move /var/lib/mysql
and /var/lib/postgresql
using systemd and symlinks on CentOS 7.2, ideally without editing conf files?
I found a way to start both that doesn't use systemd, but can't figure out why systemd fails (I realize this is technically two separate questions (mysql and postgresql), but I think the root cause is the same here).
After installing CentOS 7.2 and several packages, including mariadb (mysql) and postgresql, I moved the directories (and initialized the databases) as follows:
: the services are already stopped but just to make sure
systemctl stop mariadb postgresql
: move the directories over
mv /var/lib/pgsql /var/lib/mysql /mnt/kemptown/
: symlink back
ln -s /mnt/kemptown/mysql /mnt/kemptown/pgsql /var/lib
: initialize postgresql and mysql
postgresql-setup initdb
mysql_install_db --user=mysql
: this is a VM, so I shut it down and snapshot it here
shutdown -h now
First, the way that works:
# mysqld_safe
161221 13:16:22 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
161221 13:16:22 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
# sudo -u postgres pg_ctl -D /var/lib/pgsql/data start
server starting
Testing with mysql
and sudo -u postgres psql
connects to the databases properly.
This convinces me there's nothing fundamentally wrong with what I'm doing, since it does work. I can even leave symbolic-links=0
in /etc/my.cnf
since it apparently only cares about symlinked databases, not whether its home directory is symlinked.
Now, restoring the snapshot, here's the way that fails:
# systemctl start mariadb postgresql
Job for postgresql.service failed because the control process exited with error code. See "systemctl status postgresql.service" and "journalctl -xe" for details.
Job for mariadb.service failed because the control process exited with error code. See "systemctl status mariadb.service" and "journalctl -xe" for details.
# systemctl -l status postgresql.service
* postgresql.service - PostgreSQL database server
Loaded: loaded (/usr/lib/systemd/system/postgresql.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Wed 2016-12-21 13:23:06 EST; 2min 32s ago
Process: 2395 ExecStart=/usr/bin/pg_ctl start -D ${PGDATA} -s -o -p ${PGPORT} -w -t 300 (code=exited, status=1/FAILURE)
Process: 2385 ExecStartPre=/usr/bin/postgresql-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
Dec 21 13:23:06 babybrighton systemd[1]: Starting PostgreSQL database server...
Dec 21 13:23:06 babybrighton pg_ctl[2395]: pg_ctl: could not open PID file "/var/lib/pgsql/data/postmaster.pid": Permission denied
Dec 21 13:23:06 babybrighton systemd[1]: postgresql.service: control process exited, code=exited status=1
Dec 21 13:23:06 babybrighton systemd[1]: Failed to start PostgreSQL database server.
Dec 21 13:23:06 babybrighton systemd[1]: Unit postgresql.service entered failed state.
Dec 21 13:23:06 babybrighton systemd[1]: postgresql.service failed.
# systemctl -l status mariadb.service
* mariadb.service - MariaDB database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Wed 2016-12-21 13:23:08 EST; 2min 48s ago
Process: 2423 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=1/FAILURE)
Process: 2422 ExecStart=/usr/bin/mysqld_safe --basedir=/usr (code=exited, status=1/FAILURE)
Process: 2384 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
Main PID: 2422 (code=exited, status=1/FAILURE)
Dec 21 13:23:06 babybrighton systemd[1]: Starting MariaDB database server...
Dec 21 13:23:07 babybrighton mysqld_safe[2422]: 161221 13:23:07 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
Dec 21 13:23:07 babybrighton mysqld_safe[2422]: mkdir: cannot create directory ~/var/lib/mysql~: File exists
Dec 21 13:23:07 babybrighton mysqld_safe[2422]: Fatal error Can't create database directory '/var/lib/mysql/mysql.sock'
Dec 21 13:23:07 babybrighton systemd[1]: mariadb.service: main process exited, code=exited, status=1/FAILURE
Dec 21 13:23:08 babybrighton systemd[1]: mariadb.service: control process exited, code=exited status=1
Dec 21 13:23:08 babybrighton systemd[1]: Failed to start MariaDB database server.
Dec 21 13:23:08 babybrighton systemd[1]: Unit mariadb.service entered failed state.
Dec 21 13:23:08 babybrighton systemd[1]: mariadb.service failed.
Based on the messages above (and looking at /lib/systemd/system/mariadb.service
and /lib/systemd/system/postgresql.service
), my thoughts:
ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n
andExecStartPre=/usr/bin/postgresql-check-db-dir ${PGDATA}
appear to be doing some unnecessary checking/changing of mysql's and postgresql's home directories.It almost seems like the systemd scripts are trying to run some form of initialization, which is unnecessary, since I already did
postgresql-setup initdb
andmysql_install_db --user=mysql
earlier.The error message
cannot create directory ~/var/lib/mysql~: File exists
also suggests the systemd script is trying to do some unnecessary initialization.The error message
pg_ctl: could not open PID file "/var/lib/pgsql/data/postmaster.pid": Permission denied
confuses me and I'm not sure what to make of it.
How can I fix this without putting mysqld_safe
and sudo -u postgres pg_ctl -D /var/lib/pgsql/data start
into /etc/rc.local
or something (and then disable/mask these services in systemctl). I'd still like systemctl to see these services if possible.
One possibility is creating my own /etc/systemd/system/mariadb.service
and /etc/systemd/system/postgresql.service
since you're not supposed to edit the /lib/systemd/system/
version, but I'm unsure how to create such a file properly.
NOTE: I have not included the contents of the systemd scripts here, since they are the ones that ship with mariadb and postgresql, but can do so on request.