Centos7下修改MySQL5.7数据库文件存放路径过程

1.1 安装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、2 创建测试数据库(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>

修改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 ~]#

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 ~]#
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值