我们在使用yum安装mysql数据库的时候,他默认指定的数据路径是/var/lib/mysql,但是我们实际在给服务器分盘的时候会给服务器专门分一个盘用来放数据,所以我们需要修改mysql的数据库路径。本文介绍mysql数据库路径修改的过程。
环境
centos6.x
mysql 5.1.73
目的
将默认的数据库路径:/var/lib/mysql
修改成/data/mysql/database
假设我的mysql数据库安装在/data盘下,数据库、日志、pid、sock等文件存放路径如下:
数据库路径
/data/mysql/database
数据库日志路径
/data/mysql/log/mysqld.log
数据库pid路径
/data/mysql/run/mysqld.pid
数据库sock路径
/data/mysql/mysql.sock
开始修改
1、键入如下命令,创建相关路径:
mkdir -p /data/mysql/{database,log,run}
chown mysql:mysql /data/mysql -R
2、修改mysql配置文件,配置文件/etc/my.cnf,修改具体内容如下:
[mysqld]
datadir=/data/mysql/database
socket=/data/mysql/mysql.sock
user=mysql
#字符集为utf8
character_set_server=utf8
#绑定地址为本机,开启远程访问则修改为0.0.0.0
bind-address=127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
pid-file=/data/mysql/run/mysqld.pid
log-error=/data/mysql/log/mysqld.log
[client]
#字符集为utf8
default-character-set=utf8
[mysql]
#字符集为utf8
default-character-set=utf8
3、键入如下命令,初始化数据库:
mysql_install_db --user=mysql --basedir=/usr --datadir=/data/mysql/database
使用mysqld_safe 命令启动mysql:
[root@localhost mysql]# mysqld_safe &
[1] 4444
[root@localhost mysql]# 170812 06:39:37 mysqld_safe Logging to '/data/mysql/log/mysqld.log'.
170812 06:39:37 mysqld_safe Starting mysqld daemon with databases from /data/mysql/database
OK搞定!
使用/etc/init.d/mysqld start 命令启动:
[root@localhost mysql]# /etc/init.d/mysqld start
MySQL Daemon failed to start.
正在启动 mysqld: [失败]
启动失败!
这样就不能开机自启了。
解决办法:
关闭SELinux:
临时关闭:
setenforce 0
启动mysql:
[root@localhost mysql]# /etc/init.d/mysqld start
正在启动 mysqld: [确定]
[root@localhost mysql]#
永久关闭SELinux:
修改配置文件/etc/selinux/config文件中设置SELINUX=disabled ,如下:
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
#SELINUX=enforcing
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
# targeted - Targeted processes are protected,
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
重启Linux即可!
OK,搞定了!