Centos7.3 MySQL 5.7.24 配置数据存储位置

环境

系统 Centos7.3
MySQL版本 5.7.24
至于为什么选这个版本,emm。。。公司资源池要求的我也没办法=。=

配置

一、查看默认位置

查看 MySQL 配置文件 /etc/my.cnf

[deployer@lqas home]$ cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[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=/var/lib/mysql
socket=/var/lib/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
[deployer@lqas home]$ 

可以看到默认存储在 /var/lib/mysql

二、创建新的文件夹,作为新的 MySQL 数据存储位置

sudo mkdir /home/deployer/mysql_data
sudo mkdir /home/deployer/mysql_data/mysql

三、修改所属用户为 mysql

sudo chown -R mysql:mysql /home/deployer/mysql_data/mysql

这里留个伏笔

四、修改配置文件 datadir

先停止 MySQL 服务 systemctl stop mysqld
然后修改配置文件:

[deployer@lqas home]$ sudo vi /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[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=/home/deployer/mysql_data/mysql
socket=/var/lib/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
[deployer@lqas home]$ 

注意只修改 datadir 即可,
然后输入 :wq! 保存退出。

五、重启 MySQL 服务

[deployer@lqas home]$ systemctl restart mysqld
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.

发现报错,

  • 然后查看 systemctl status mysqld.service ,并没有什么提示;
  • 再查看 journalctl -xe,提示 Failed password for imonitor from 1XX.1XX.1XX.XXX port 50586 ssh2
    看起来好像是密码不对或者权限问题
  • 再查看日志文件 cat /var/log/mysqld.log ,没有发现 error;

然后看了网上很多解决办法,包括创建文件、搞什么软连接等等,但是对于我这个环境没有作用,查了一天,看到了这个文章 https://blog.csdn.net/u012328159/article/details/80115096,给了我提示。

六、解决报错

前面第三步我只修改了 /home/deployer/mysql_data/mysql 的所属用户,根据文章把上级目录 /home/deployer/mysql_data 的权限也修改了:

sudo chown -R mysql:mysql /home/deployer/mysql_data

然后再重启 MySQL 服务发现成功了

七、验证位置是否修改成功

进入 MySQL,查看存储位置(刚安装好需要提前配置 MySQL 密码,可以点击MySQL 设置密码查看):

mysql> select @@datadir;
+----------------------------------+
| @@datadir                        |
+----------------------------------+
| /home/deployer/mysql_data/mysql/ |
+----------------------------------+
1 row in set (0.00 sec)

可以看到已经修改成功了!

撒花~~~

猜你喜欢

转载自blog.csdn.net/weixin_41474364/article/details/102738936