Centos7上修改mysql数据目录

时间:2021-10-23 15:20:27
通过yum安装的mysql,启动和增加数据库,增加数据如下:
[root@wucl-4 lib]# systemctl start mariadb
[root@wucl-4 lib]# mysql -uroot
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.52-MariaDB MariaDB Server Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| abc |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec) MariaDB [(none)]> use abc;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A Database changed
MariaDB [abc]> show tables;
+---------------+
| Tables_in_abc |
+---------------+
| abc |
+---------------+
1 row in set (0.00 sec) MariaDB [abc]> select * from abc;
+------+---------+
| id | name |
+------+---------+
| 1 | baoshan |
+------+---------+
1 row in set (0.00 sec) MariaDB [abc]> show variables like 'datadir%';
+---------------+-----------------+
| Variable_name | Value |
+---------------+-----------------+
| datadir | /var/lib/mysql/ |
+---------------+-----------------+
1 row in set (0.01 sec) 现在将mysql数据目录换成/var/lib/mysql8
操作步骤如下:
1. 停止数据库systemctl start mariadb
2. 修改配置文件/etc/my.conf
[mysqld]
datadir=/var/lib/mysql8
socket=/var/lib/mysql8/mysql.sock
3. 新建mysql8文件夹,将mysql原数据目录中的文件复制到mysql8目录中
[root@wucl-4 lib]# pwd
/var/lib
[root@wucl-4 lib]# mkdir mysql8
[root@wucl-4 lib]# cp -rf mysql/*
mysql8/
4. 修改mysql8目录的所属用户和组
chown -R mysql:mysql mysql8/
5. 启动mysql数据库
systemctl start mariadb
6. 访问数据库,查看数据时候迁移成功,查看现在数据目录

[root@wucl-4 lib]# mysql -uroot -S /var/lib/mysql8/mysql.sock
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.52-MariaDB MariaDB Server Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| abc |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec) MariaDB [(none)]> use abc;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A Database changed
MariaDB [abc]> show tables;
+---------------+
| Tables_in_abc |
+---------------+
| abc |
+---------------+
1 row in set (0.00 sec) MariaDB [abc]> select * from abc;
+------+---------+
| id | name |
+------+---------+
| 1 | baoshan |
+------+---------+
1 row in set (0.00 sec) MariaDB [abc]> show variables like 'datadir%';
+---------------+------------------+
| Variable_name | Value |
+---------------+------------------+
| datadir | /var/lib/mysql8/ |
+---------------+------------------+
1 row in set (0.00 sec) 前后折腾了好久,mark一下。