内容简介:MySQL数据库默认的数据库文件位于/var/lib/mysql下,有时候由于存储规划等原因,需要更改MySQL数据库的数据存储目录。下文总结整理了实践过程的操作步骤。在更改MySQL的数据目录前,必须关闭MySQL服务。并不是所有版本都包含有my.cnf这个配置文件,在MySQL 5.5版本,我就找不到my.cnf这个配置文件, 而有些MySQL版本该文件位于/usr/my.cnf,如果/etc/目录下没有my.cnf配置文件,请到/usr/share/mysql/下找到*.cnf文件,拷贝其中一个到/
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 ] [root@DB-Server mysql]#
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- MySQL更改数据库数据存储目录
- Ubuntu上更改MySQL数据库数据存储目录
- Oracle如何更改表空间的数据文件位置详解
- MSSQL 使用Change Tracking(更改跟踪)同步数据
- c# – 如何将数据库更改传播到我的.edmx文件?
- superset 性能优化1-已经使用中的superset更改默认数据源sqlite到mysql
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Spring in Action
Craig Walls / Manning Publications / 2011-6-29 / USD 49.99
Spring in Action, Third Edition has been completely revised to reflect the latest features, tools, practices Spring offers to java developers. It begins by introducing the core concepts of Spring and......一起来看看 《Spring in Action》 这本书的介绍吧!