Friday, August 3, 2018

How to Change MySQL Data Directory to a New Location in Linux 6

1. connect mysql database to identify data dir location

[root@oracle var]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.21 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select @@datadir;
+-----------------+
| @@datadir       |
+-----------------+
| /var/lib/mysql/ |
+-----------------+
mysql> exit
Bye

2. Stop mysql database to change data dir location

[root@oracle var]# service mysqld stop
Stopping mysqld:                                           [  OK  ]

Run below command sync data to new location

[root@oracle var]# rsync -av /var/lib/mysql /u01/mysql_newlocation
sending incremental file list
mysql/
mysql/.bash_history -> /dev/null
mysql/.bashrc
mysql/.mysql_history
mysql/auto.cnf
mysql/ca-key.pem
mysql/ca.pem
mysql/client-cert.pem
mysql/client-key.pem
mysql/dead.letter
mysql/ib_buffer_pool
mysql/ib_logfile0
mysql/ib_logfile1
mysql/ibdata1
mysql/mysql-init
mysql/mysql-init1
mysql/ninfo
.
.

etc

sent 210708737 bytes  received 8791 bytes  84287011.20 bytes/sec
total size is 210649498  speedup is 1.00

Move /var/lib/mysql folder old

[root@oracle var]# mv /var/lib/mysql /var/lib/mysql.bak

3. Change datadir location in configuration file

vi /etc/my.cnf

[mysqld]


datadir=/u01/mysql_newlocation/mysql
socket=/u01/mysql_newlocation/mysql/mysql.sock

## New entry for client configuration 

[client]
port=3306
socket=/u01/mysql_newlocation/mysql/mysql.sock

4. Restart mysql

[root@oracle lib]# service mysqld start
Starting mysqld:                                           [  OK  ]

5. Verify new datadir 

[root@oracle mysql]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.21 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select @@datadir;
+-------------------------------+
| @@datadir                     |
+-------------------------------+
| /u01/mysql_newlocation/mysql/ |
+-------------------------------+
1 row in set (0.00 sec)

mysql> exit
Bye

No comments:

Post a Comment