centos7系统更换mysql数据存储路径,无法启动问题

京漂大叔 2020-01-08 AM 5020℃ 0条

第一次再centos7的系统下配置MySQL,遇到的坑不少,一一记录下,避免下次再犯
默认安装好mysql之后,数据存储的路径在/var/lib/mysql下面,下面我们修改数据的存储路径
编辑/etc/my.cnf配置文件
1、先停止数据库的运行

systemctl stop mariadb //mysql在centos7名字变成了mariadb

2、创建新的数据存储目录

mkdir /data/mysql/

给这个目录加上读写权限:

chmod 777 /mysqldata

把 /var/lib/mysql 整个目录复制到 /data/mysql/

cp -r /var/lib/mysql/* /data/mysql/

/var/log/mariadb/复制到/data/log/mariadb
cp -a /var/log/mariadb/ /data/log/mariadb

2、修改mysql配置文件,把[mysqld]下的配置项datadir注释掉(以防有问题再改回来),新加行改为新的数据存储路径

[mysqld]
#datadir=/var/lib/mysql
datadir=/mysqldata
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
 
[mysqld_safe]
#log-error=/var/log/mariadb/mariadb.log
log-error=/mysqldata/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
 
#
# include all files from the config directory
#

3、保存文件退出。
修改 /datamysql 目录的拥有者

chown -R mysql:mysql /datamysql

4、启动 Mariadb 服务

systemctl start mariadb.service

重启的时候,报错

[root@localhost www]# systemctl start mariadb
Job for mariadb.service failed because the control process exited with error code. See "systemctl status mariadb.service" and "journalctl -xe" for details.

然后执行systemctl status mariadb.service查看状态,如下

[root@localhost www]# systemctl status mariadb.service
● mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since 三 2020-01-08 09:18:40 CST; 3s ago
  Process: 17001 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=1/FAILURE)
  Process: 17000 ExecStart=/usr/bin/mysqld_safe --basedir=/usr (code=exited, status=0/SUCCESS)
  Process: 16965 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
 Main PID: 17000 (code=exited, status=0/SUCCESS)

1月 08 09:18:39 localhost.localdomain systemd[1]: Starting MariaDB database server...
1月 08 09:18:39 localhost.localdomain mariadb-prepare-db-dir[16965]: Database MariaDB is probably initialized in /www/data already, nothing is done.
1月 08 09:18:39 localhost.localdomain mariadb-prepare-db-dir[16965]: If this is not the case, make sure the /www/data is empty before running mariadb-prepare-db-dir.
1月 08 09:18:39 localhost.localdomain mysqld_safe[17000]: 200108 09:18:39 mysqld_safe Logging to '/www/log/mariadb/mariadb.log'.
1月 08 09:18:39 localhost.localdomain mysqld_safe[17000]: 200108 09:18:39 mysqld_safe Starting mysqld daemon with databases from /www/data
1月 08 09:18:40 localhost.localdomain systemd[1]: mariadb.service: control process exited, code=exited status=1
1月 08 09:18:40 localhost.localdomain systemd[1]: Failed to start MariaDB database server.
1月 08 09:18:40 localhost.localdomain systemd[1]: Unit mariadb.service entered failed state.
1月 08 09:18:40 localhost.localdomain systemd[1]: mariadb.service failed.

然后进入到mysql的日志,有一句警告

200108  9:18:39 [Warning] Can't create test file /www/data/localhost.lower-test

百度之后,通过下面的方法执行命令之后,在执行启动mysql命令,问题解决
**setenforce 0
但最好使用一个永久方法,以便在重启后继续不要这货。
修改/etc/selinux/config文件中设置SELINUX=disabled ,然后重启或等待下次重启。**

标签: mysql, centos7

非特殊说明,文章均为原创。

评论啦~