修改MySQL数据存储目录

警告
本文最后更新于 2018-12-11,文中内容可能已过时。

MySQL默认的数据存储路径是在 /var 下面。由于该目录可能设置空间不大,后期会影响使用,我准备把数据存储在 /data/DataBase 这个目录下面,用于集中的管理,后期也可以把磁盘空间增加到足够大。

本文参考了这篇文章

确认当前存储路径

1
2
3
[root@localhost ~]# mysqladmin -u root -p variables | grep datadir
Enter password:
| datadir                                           | /var/lib/mysql/

停止 MySQL 服务

CentOS7 下,统一使用 systemctl 来管理所有的服务进程。同时,需要注意的是,从 MySQL5.5 开始,默认的存储引擎改成了 mariadb。因此,为了停止 MySQL 服务进程,我们可以使用命令:

1
2
[root@localhost ~]# systemctl stop mariadb.service
Warning: mariadb.service changed on disk. Run 'systemctl daemon-reload' to reload units.

把数据移动到指定目录

事先在 /data/ 文件夹下面建立 /data/DataBase 用来专门存储 MySQL 所有的数据。我们需要把原来的数据库移动到该目录下:

1
[root@localhost data]# mv /var/lib/mysql /data/DataBase/

修改配置文件

一般是在 /etc/my.cnf 这个文件:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
vim /etc/my.cnf

 [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=/var/lib/mysql
datadir=/data/DataBase/mysql
## socket=/var/lib/mysql/mysql.sock
socket=/data/DataBase/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]
socket=/data/DataBase/mysql/mysql.sock

检查是否已经生效

1
2
3
4
[root@localhost data]# systemctl start mariadb.service
[root@localhost data]# mysqladmin -u root -p variables | grep datadir
Enter password:
| datadir                                           | /data/DataBase/mysql/

相关内容

william 支付宝支付宝
william 微信微信
0%