Centos7下修改MySQL5.7數據庫文件存放路徑過程

1 測試環境搭建

1.1 安裝操作系統Centos7

[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
[root@localhost ~]#

1.2 安裝Mysql

######################## 安裝Mysql ########################
[root@localhost home]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
......
[root@localhost home]# yum -y install mysql57-community-release-el7-10.noarch.rpm
......
[root@localhost home]# yum -y install mysql-community-server
......

####################### 查看Mysql版本 ######################
[root@localhost home]# yum list installed | grep "mysql"
mysql-community-client.x86_64        5.7.29-1.el7                   @mysql57-community
mysql-community-common.x86_64        5.7.29-1.el7                   @mysql57-community
mysql-community-libs.x86_64          5.7.29-1.el7                   @mysql57-community
mysql-community-server.x86_64        5.7.29-1.el7                   @mysql57-community
mysql57-community-release.noarch     el7-10                         @/mysql57-community-release-el7-10.noarch
[root@localhost home]#
......

####################### 啓動Mysql ##########################
[root@localhost home]# service mysqld start
Redirecting to /bin/systemctl start mysqld.service
[root@localhost home]#
......

################# 查看Mysql安裝後臨時密碼 ###################
[root@localhost ~]# grep "temporary password" /var/log/mysqld.log
2020-04-18T07:29:50.285076Z 1 [Note] A temporary password is generated for root@localhost: psjaAeaU+0g!
[root@localhost ~]#
......

################# 通過上述臨時密碼登錄MySQL ################
[root@localhost ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.29

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

################ 更改root登錄密碼 #####################
mysql> alter user 'root'@'localhost' identified by 'P@ss123456';
Query OK, 0 rows affected (0.00 sec)

mysql>

注意:(1)如果之前安裝了Mysql,卸載後重新安裝Mysql啓動後可能會發生/var/log/mysqld.sql中沒有默認密碼生成的問題,此時可以刪除/var/lib/mysql 整個文件夾,然後重啓mysqld服務就可以讓整個數據庫重新初始化並在/var/log/mysqld.log中生成默認密碼了。

1.3 創建測試數據庫(test)和表(student)

mysql> create database test;
Query OK, 1 row affected (0.00 sec)

mysql> use test;
Database changed
mysql> create table student(id varchar(10) not null primary key, name varchar(10));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into student(id,name)values('111', 'AAA');
Query OK, 1 row affected (0.02 sec)

mysql> insert into student(id,name)values('222', 'BBB');
Query OK, 1 row affected (0.00 sec)

mysql> select * from student;
+-----+------+
| id  | name |
+-----+------+
| 111 | AAA  |
| 222 | BBB  |
+-----+------+
2 rows in set (0.00 sec)

mysql>

2 修改Mysql數據存儲路徑到其他文件夾

2.1 查看當前Mysql數據存儲路徑,如下可知當前數據存放路徑爲/var/lib/mysql

#################### 查看/etc/my.cnf 配置文件內容 #######################
[root@localhost ~]# 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
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

############### 查看/var/lib/mysql路徑下文件列表 ####################
[root@localhost ~]# ls /var/lib/mysql
auto.cnf    client-cert.pem  ibdata1      ibtmp1      mysql.sock.lock     public_key.pem   sys
ca-key.pem  client-key.pem   ib_logfile0  mysql       performance_schema  server-cert.pem  test
ca.pem      ib_buffer_pool   ib_logfile1  mysql.sock  private_key.pem     server-key.pem
[root@localhost ~]#

2.2 停止mysqld服務,創建新的mysql數據存放路徑/home/data

################# 停止mysqld服務 #####################
[root@localhost ~]# service mysqld stop
Redirecting to /bin/systemctl stop mysqld.service
[root@localhost ~]#

########### 創建新的mysql數據存放路徑 #################
[root@localhost ~]# mkdir -p /home/data/
[root@localhost ~]# ls /home/data/
[root@localhost ~]#

2.3 修改/etc/my.cnf,在[mysqld]選項組下配置新的文件路徑

[root@localhost ~]# 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=/home/data/mysql
socket=/home/data/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
[root@localhost ~]#

如上所示,修改了datadir=/home/data/mysql, socket=/home/data/mysql/mysql.sock

2.4 移動/var/lib/mysql整個目錄到新的文件夾/home/data/

[root@localhost ~]# mv /var/lib/mysql /home/data/
[root@localhost ~]# ls /home/data/mysql/
auto.cnf    ca.pem           client-key.pem  ibdata1      ib_logfile1  performance_schema  public_key.pem   server-key.pem  test
ca-key.pem  client-cert.pem  ib_buffer_pool  ib_logfile0  mysql        private_key.pem     server-cert.pem  sys
[root@localhost ~]#

注意:我這使用了mv來移動mysql文件夾,請保持一致

2.5 啓動mysqld服務,解決因selinux開啓導致的啓動錯誤

[root@localhost ~]# service mysqld start
Redirecting to /bin/systemctl start mysqld.service
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.

可以看到系統報錯,這是因爲沒有關閉selinux導致的,關閉selinux然後重新啓動mysqld服務,成功

[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
Permissive
[root@localhost ~]#
[root@localhost ~]# service mysqld start
Redirecting to /bin/systemctl start mysqld.service
[root@localhost ~]# service mysqld status
Redirecting to /bin/systemctl status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2020-04-18 04:06:21 EDT; 3min 25s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 1964 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 1947 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 1967 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─1967 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Apr 18 04:06:20 localhost.localdomain systemd[1]: Starting MySQL Server...
Apr 18 04:06:21 localhost.localdomain systemd[1]: Started MySQL Server.
[root@localhost ~]#

2.6 使用mysql -u root -p來登錄mysql,發生因未配置[client]選項組中參數導致的錯誤

[root@localhost ~]# mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
[root@localhost ~]#

如上,發生了錯誤

2.7 修改/etc/my.cnf,修改[client]選項組下的配置信息

[root@localhost ~]# 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=/home/data/mysql
socket=/home/data/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

[client]
socket=/home/data/mysql/mysql.sock

[root@localhost ~]#

如上所示,添加了[client]選項組下面的socket配置信息

2.8 重啓mysqld服務(service mysqld restart),使用mysql -u root -p登錄mysql並測試成功

[root@localhost ~]#
[root@localhost ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.29 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from student;
+-----+------+
| id  | name |
+-----+------+
| 111 | AAA  |
| 222 | BBB  |
+-----+------+
2 rows in set (0.00 sec)

mysql>

2.9 重啓操作系統,發現mysqld服務啓動啓動時發生錯誤

[root@localhost ~]# service mysqld status
Redirecting to /bin/systemctl status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: failed (Result: start-limit) since Sat 2020-04-18 04:27:25 EDT; 8s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 1749 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=1/FAILURE)
  Process: 1732 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)

Apr 18 04:27:24 localhost.localdomain systemd[1]: Failed to start MySQL Server.
Apr 18 04:27:24 localhost.localdomain systemd[1]: Unit mysqld.service entered failed state.
Apr 18 04:27:24 localhost.localdomain systemd[1]: mysqld.service failed.
Apr 18 04:27:25 localhost.localdomain systemd[1]: mysqld.service holdoff time over, scheduling restart.
Apr 18 04:27:25 localhost.localdomain systemd[1]: Stopped MySQL Server.
Apr 18 04:27:25 localhost.localdomain systemd[1]: start request repeated too quickly for mysqld.service
Apr 18 04:27:25 localhost.localdomain systemd[1]: Failed to start MySQL Server.
Apr 18 04:27:25 localhost.localdomain systemd[1]: Unit mysqld.service entered failed state.
Apr 18 04:27:25 localhost.localdomain systemd[1]: mysqld.service failed.

上面啓動失敗是selinux在Linux重啓後重新啓用了造成的

2.10 修改selinux配置文件/etc/selinux/config,讓selinux永久工作在permissive模式下

[root@localhost ~]# vi /etc/selinux/config
[root@localhost ~]# cat /etc/selinux/config

# 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=permissive
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted


[root@localhost ~]#

2.11 再次重啓操作系統,查看mysqld狀態,並登錄測試數據庫,一切正常

keycer@LAPTOP-8JTG7IOM MINGW64 ~/Desktop
$ ssh [email protected]
[email protected]'s password:
Last login: Sat Apr 18 04:27:29 2020 from laptop-8jtg7iom
[root@localhost ~]# service mysqld status
Redirecting to /bin/systemctl status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2020-04-18 04:34:46 EDT; 27s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 1205 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 1162 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 1228 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─1228 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Apr 18 04:34:45 localhost.localdomain systemd[1]: Starting MySQL Server...
Apr 18 04:34:46 localhost.localdomain systemd[1]: Started MySQL Server.
[root@localhost ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.29 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from student;
+-----+------+
| id  | name |
+-----+------+
| 111 | AAA  |
| 222 | BBB  |
+-----+------+
2 rows in set (0.00 sec)

mysql>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章