Contents...
Databases are constantly growing and it is likely to outgrow your / partition at some point, especially these days in the age of “the cloud” where you may be limited to a small / partition, but can pay to add bulk storage, or attach more disks. Alternatively, you may wish to move your database to a separate location because you want to configure some sort of snapshotting technique, such as with ZFS or LVM.
In this tutorial I will show you how you can change or move your data directory to new location on CentOS 7.
Step #1: Move MySQL Data Directory
Before moving MySQL’s data directory lets see the current location using mysql command line as shown below.
# mysql -u root -p mysql> select @@datadir; +-----------------+ | @@datadir | +-----------------+ | /var/lib/mysql/ | +-----------------+ 1 row in set (0.00 sec)
In the above output in can see the MySQL default data direcror /var/lib/mysql/. So we are going to move this directory. Once you’ve confirmed this, type exit and press “ENTER” to leave the monitor:
mysql> exit
Before changing the data directory you will need to shut down MySQL service.
# systemctl stop mysqld
systemctl doesn’t display the outcome of all service management commands, so if you want to be sure you’ve succeeded, use the following command:
# systemctl status mysqld Jun 17 11:24:20 centos-512mb-nyc1-01 systemd[1]: Stopped MySQL Community Server.
Now MySQL service is shut down lets start copy the existing data directory to new location with rsync command using with –a option to preserves the permissions and other directory properties while-v provides verbose output so you can follow the progress.
# rsync -av /var/lib/mysql /mnt/data/
Once the rsync is complete, rename the current folder with a .bak extension and keep it until we’ve confirmed the move was successful. By re-naming it, we’ll avoid confusion that could arise from files in both the new and the old location.
# mv /var/lib/mysql /var/lib/mysql.bak
Step #2: Pointing to the New Data Location
By default MySQL data directory set to /var/lib/mysql in the /etc/my.cnf file. Now edit this file to reflect the new data directory as shown below.
# vim /etc/my.cnf [mysqld] . . . datadir=/mnt/data/mysql socket=/mnt/data/mysql/mysql.sock . . .
Save and close file.
After updating the existing lines, we’ll need to add configuration for the mysql client. Insert the following settings at the bottom of the file so it won’t split up directives in the [mysqld] block.
# vim /etc/my.cnf [client] port=3306 socket=/mnt/volume-nyc1-01/mysql/mysql.sock
Save and close file.
Step #3: Restart MySQL Service
Now restart the MySQL service after updated its configuration to use the new location.
# systemctl start mysqld # systemctl status mysqld
To make sure that the new data directory is indeed in use, start the MySQL monitor.
# mysql -u root -p mysql> select @@datadir; +----------------------------+ | @@datadir | +----------------------------+ | /mnt/data/mysql/ | +----------------------------+ 1 row in set (0.01 sec)
In the above output you can see the MySQL new data directory location is /mnt/data/mysql/.
If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at LookLinux Submit Article. Stay connected to Facebook.
Followed this blog for MySQL5.7 on CentOS 8 to move default /var/lib/mysql to my NFSv4.1 mount point /mnt/mysql57 but at the time of starting mysqld, i got the following error:
[root@mysql-test2 /]# systemctl start mysqld
Job for mysqld.service failed because the control process exited with error code.
See “systemctl status mysqld.service” and “journalctl -xe” for details.
[root@mysql-test2 /]# journalctl -xe
Jan 19 11:01:23 mysql-test2 setroubleshoot[44924]: SELinux is preventing /usr/sbin/mysqld from write access on the directory mysql.
***** Plugin catchall (100. confidence) suggests **************************
If you believe that mysqld should be allowed write access on the mysql directory by default.
Then you should report this as a bug.
You can generate a local policy module to allow this access.
Do
allow this access for now by executing:
# ausearch -c ‘mysqld’ –raw | audit2allow -M my-mysqld
# semodule -X 300 -i my-mysqld.pp
Jan 19 11:01:23 mysql-test2 setroubleshoot[44924]: AnalyzeThread.run(): Set alarm timeout to 10
Jan 19 11:01:23 mysql-test2 setroubleshoot[44924]: AnalyzeThread.run(): Cancel pending alarm
Jan 19 11:01:24 mysql-test2 setroubleshoot[44924]: SELinux is preventing /usr/sbin/mysqld from write access on the directory mysql. For complete SELinux mes>
Jan 19 11:01:24 mysql-test2 setroubleshoot[44924]: SELinux is preventing /usr/sbin/mysqld from write access on the directory mysql.
***** Plugin catchall (100. confidence) suggests **************************
If you believe that mysqld should be allowed write access on the mysql directory by default.
Then you should report this as a bug.
You can generate a local policy module to allow this access.
Do
allow this access for now by executing:
# ausearch -c ‘mysqld’ –raw | audit2allow -M my-mysqld
# semodule -X 300 -i my-mysqld.pp
Jan 19 11:01:24 mysql-test2 setroubleshoot[44924]: AnalyzeThread.run(): Set alarm timeout to 10
Jan 19 11:01:24 mysql-test2 setroubleshoot[44924]: AnalyzeThread.run(): Cancel pending alarm
Jan 19 11:01:24 mysql-test2 setroubleshoot[44924]: failed to retrieve rpm info for /mnt/mysql57/mysql/ibdata1
Jan 19 11:01:24 mysql-test2 setroubleshoot[44924]: SELinux is preventing /usr/sbin/mysqld from getattr access on the file /mnt/mysql57/mysql/ibdata1. For co>
Jan 19 11:01:24 mysql-test2 setroubleshoot[44924]: SELinux is preventing /usr/sbin/mysqld from getattr access on the file /mnt/mysql57/mysql/ibdata1.
***** Plugin catchall (100. confidence) suggests **************************
If you believe that mysqld should be allowed getattr access on the ibdata1 file by default.
Then you should report this as a bug.
You can generate a local policy module to allow this access.
Do
allow this access for now by executing:
# ausearch -c ‘mysqld’ –raw | audit2allow -M my-mysqld
# semodule -X 300 -i my-mysqld.pp
Jan 19 11:01:24 mysql-test2 setroubleshoot[44924]: AnalyzeThread.run(): Set alarm timeout to 10
lines 2693-2736/2736 (END)
The content of my /etc/my.cnf is as shown below
[root@mysql-test2 /]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/mnt/mysql57/mysql
socket=/mnt/mysql57/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[client]
port=3306
socket=/mnt/mysql57/mysql/mysql.sock
[nutanix123@mysql-test2 mysql57]$ df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 7.7G 0 7.7G 0% /dev
tmpfs 7.8G 0 7.8G 0% /dev/shm
tmpfs 7.8G 9.5M 7.7G 1% /run
tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
/dev/mapper/cl-root 70G 5.7G 65G 9% /
/dev/mapper/cl-home 122G 911M 121G 1% /home
/dev/sda1 1014M 239M 776M 24% /boot
tmpfs 1.6G 1.2M 1.6G 1% /run/user/42
tmpfs 1.6G 6.9M 1.6G 1% /run/user/1000
XXXXXXXXXXXXXXXXXX:/mysql-centos 2.0T 800M 2.0T 1% /mnt/mysql57
(Mount path IP is masked for security reasons)
Followed this on a new Fedora 28 installation, but got this errorÖ
# systemctl start mysqld
Job for mysqld.service failed because the control process exited with error code.
See “systemctl status mysqld.service” and “journalctl -xe” for details.
[root@leia2 ~]# systemctl status mysqld.service
● mysqld.service – MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Mon 2018-06-18 21:29:21 CEST; 7s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 13948 ExecStart=/usr/sbin/mysqld $MYSQLD_OPTS (code=exited, status=1/FAILURE)
Process: 13931 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 13948 (code=exited, status=1/FAILURE)
Status: “SERVER_BOOTING”
Error: 13 (Permission denied)
Jun 18 21:29:21 leia2.thommym.st systemd[1]: Starting MySQL Server…
Jun 18 21:29:21 leia2.thommym.st systemd[1]: mysqld.service: Main process exited, code=exited, status=1/FAILURE
Jun 18 21:29:21 leia2.thommym.st systemd[1]: mysqld.service: Failed with result ‘exit-code’.
Jun 18 21:29:21 leia2.thommym.st systemd[1]: Failed to start MySQL Server.
# systemctl status mysqld
● mysqld.service – MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Mon 2018-06-18 21:29:21 CEST; 1min 48s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 13948 ExecStart=/usr/sbin/mysqld $MYSQLD_OPTS (code=exited, status=1/FAILURE)
Process: 13931 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 13948 (code=exited, status=1/FAILURE)
Status: “SERVER_BOOTING”
Error: 13 (Permission denied)
Jun 18 21:29:21 leia2.thommym.st systemd[1]: Starting MySQL Server…
Jun 18 21:29:21 leia2.thommym.st systemd[1]: mysqld.service: Main process exited, code=exited, status=1/FAILURE
Jun 18 21:29:21 leia2.thommym.st systemd[1]: mysqld.service: Failed with result ‘exit-code’.
Jun 18 21:29:21 leia2.thommym.st systemd[1]: Failed to start MySQL Server.