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

发布时间:2023年12月26日

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

(1)如果之前安装了Mysql,卸载后重新安装Mysql启动后可能会发生/var/log/mysqld.sql中没有默认密码生成的问题,此时可以删除/var/lib/mysql 整个文件夹,然后重启mysqld服务就可以让整个数据库重新初始化并在/var/log/mysqld.log中生成默认密码了。

查看mysql数据库文件存放路径:

mysql> show global variables like "%datadir%";

查看MySQL安装目录

ps -ef|grep mysql

修改Mysql数据存储路径到其他文件夹

查看当前Mysql数据存储路径,如下可知当前数据存放路径为/var/lib/mysql

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html


[mysqld]
# skip-grant-tables
#
# 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 the leading "# " to disable binary logging
# Binary logging captures changes between backups and is enabled by
# default. It's default setting is log_bin=binlog
# disable_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
#
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password

#sql_mode="MYSQL40"
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
default_authentication_plugin=mysql_native_password



# datadir=/var/lib/mysql
# socket=/var/lib/mysql/mysql.sock


log-error=/var/log/mysqld.log

停止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 ~]#

修改/etc/my.cnf,在[mysqld]选项组下配置新的文件路径

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html


[mysqld]
# skip-grant-tables
#
# 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 the leading "# " to disable binary logging
# Binary logging captures changes between backups and is enabled by
# default. It's default setting is log_bin=binlog
# disable_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
#
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password

#sql_mode="MYSQL40"
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
default_authentication_plugin=mysql_native_password



# datadir=/var/lib/mysql
# socket=/var/lib/mysql/mysql.sock

datadir=/home/data/mysql
socket=/home/data/mysql/mysql.sock

log-error=/var/log/mysqld.log

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

移动/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文件夹,请保持一致

参考:https://blog.csdn.net/zgrjkflmkyc/article/details/105571586

文章来源:https://blog.csdn.net/weixin_46160781/article/details/135232324
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。