查看原文
其他

MySQL 更改数据库数据存储目录

ImportNew 2019-12-01

(点击上方公众号,可快速关注)


来源:潇湘隐者 ,

www.cnblogs.com/kerrycode/p/4371938.html


MySQL数据库默认的数据库文件位于 /var/lib/mysql 下,有时候由于存储规划等原因,需要更改 MySQL 数据库的数据存储目录。下文总结整理了实践过程的操作步骤。


1 确认MySQL数据库存储目录


[root@DB-Server tmp]# mysqladmin -u root -p variables | grep datadir

Enter password: 

| datadir | /var/lib/mysql/


2 关闭MySQL服务


在更改MySQL的数据目录前,必须关闭MySQL服务。


方式1:


[root@DB-Server ~]# service mysql status

MySQL running (9411)[ OK ]

[root@DB-Server ~]# service mysql stop

Shutting down MySQL..[ OK ]

[root@DB-Server ~]#



方式2:


[root@DB-Server ~]# /etc/rc.d/init.d/mysql status

MySQL running (8900)[ OK ]

[root@DB-Server ~]# /etc/rc.d/init.d/mysql stop

Shutting down MySQL..[ OK ]

[root@DB-Server ~]#


3 创建新的数据库存储目录


[root@DB-Server ~]# cd /u01

[root@DB-Server u01]# mkdir mysqldata


4 移动MySQL数据目录到新位置


[root@DB-Server ~]# mv /var/lib/mysql /u01/mysqldata/


5 修改配置文件my.cnf


并不是所有版本都包含有 my.cnf 这个配置文件,在 MySQL 5.5 版本,我就找不到 my.cnf 这个配置文件, 而有些 MySQL 版本该文件位于 /usr/my.cnf,如果 /etc/ 目录下没有 my.cnf 配置文件,请到 /usr/share/mysql/ 下找到 *.cnf 文件,拷贝其中一个到 /etc/ 并改名为 my.cnf 中。命令如下:



[root@DB-Server mysql]# cp /usr/share/mysql/my-medium.cnf /etc/my.cnf


编辑 /etc/my.cnf 文件,修改参数 socket



MySQL 5.5 版本


# The following options will be passed to all MySQL clients

[client]

#password       = your_password

port            = 3306

socket          = /u01/mysqldata/mysql/mysql.sock

 

# Here follows entries for some specific programs

 

# The MySQL server

[mysqld]

port            = 3306

socket          = /u01/mysqldata/mysql/mysql.sock

skip-external-locking

key_buffer_size = 16M

max_allowed_packet = 1M

table_open_cache = 64

sort_buffer_size = 512K

net_buffer_length = 8K

read_buffer_size = 256K

read_rnd_buffer_size = 512K

myisam_sort_buffer_size = 8M



6 修改启动脚本 /etc/init.d/mysql


将参数 datadir 修改为 datadir=/u01/mysqldata/mysql/



7 启动MySQL服务并验证MySQL数据库路径


[root@DB-Server ~]# service mysql start

Starting MySQL..[  OK  ]

[root@DB-Server ~]# mysqladmin -u root -p variables | grep datadir

Enter password: 

| datadir        | /u01/mysqldata/mysql/


我的疑问


1 在修改数据库的存储目录前,/var/lib/mysql/ 目录下根本没有 mysql.sock 文件,安装上面配置后,就会生成 mysql.sock 文件。


关于 mysql.sock 文件,搜索了一下资料:mysql.sock 是用于 socket 连接的文件。也就是只有你的守护进程启动起来这个文件才存在。但是你的 mysql 程序(这个程序是客户端,服务器端是 mysqld)可以选择是否使用 mysql.sock 文件来连接(因为这个方法只适合在 Unix 主机上面连接本地的 mysqld),对于非本地的任何类型的主机。


那么这个文件是否一定需要的呢? 这个需要进一步了解清楚。


2 我在网上看有些网友总结的修改 MySQL 数据路径,有些需要给新建的目录的权限做一些处理,而有些有不用对目录权限进行授权,我没有处理,也没有什么问题。到底要不要对新的数据库目录授权呢?


3 我在 MySQL_5.6.20 这个版本测试时,不修改 my.cnf,只修改启动脚本 /etc/init.d/mysql,也完全没有啥问题。也没有 myssql.sock 文件生成。


4 注意如果没有禁用 SElinux,修改MySQL的数据路径后启动MySQL服务会遇到一些错误。关于这个的解释是后台服务都需要有对相应目录的对应权限,而 mysql 的默认路径 /var/lib/mysql 已经添加了相应的策略,修改路径后由于没有相应的策略,导致后台进程读取文件被 SElinux 阻止,从而出现权限错误。 所以要么关闭 SElinux 或修改文件安全上下文。


[root@DB-Server mysql]# /etc/init.d/mysql start

Starting MySQL....The server quit without updating PID file (/u01/mysqldata/mysql//DB-Server.localdomain.pid).[FAILED]

[root@DB-Server mysql]# 

[root@DB-Server mysql]# chcon -R -t mysqld_db_t /u01/mysqldata/mysql/

[root@DB-Server mysql]# /etc/init.d/mysql start

Starting MySQL.[ OK ]


参考资料


  • http://database.ctocio.com.cn/tips/449/7566949.shtml

  • www.linuxidc.com/Linux/2012-12/75647.htm

  • http://blog.csdn.net/hellyhe/article/details/8309470


【关于投稿】


如果大家有原创好文投稿,请直接给公号发送留言。


① 留言格式:
【投稿】+《 文章标题》+ 文章链接

② 示例:
【投稿】《不要自称是程序员,我十多年的 IT 职场总结》:http://blog.jobbole.com/94148/

③ 最后请附上您的个人简介哈~



看完本文有收获?请转发分享给更多人

关注「ImportNew」,提升Java技能

    您可能也对以下帖子感兴趣

    文章有问题?点此查看未经处理的缓存