Bootstrap

CentOS7 安装 MySQL5.7.35 并且 更换默认的数据目录

一、安装包准备

下载MySQL5.7.35

下载地址:https://dev.mysql.com/downloads/mysql/5.7.html
其他版本:https://downloads.mysql.com/archives/community/

下载完整的压缩包:
在这里插入图片描述

压缩包传到服务器

目录:/home/sys/mysql-5.7.35-1.el7.x86_64.rpm-bundle.tar
在这里插入图片描述

二、安装MySQL5.7.35

CentOS 7.6 关闭防火墙

查看防火墙状态
systemctl status firewalld.service

#关闭运行的防火墙
systemctl stop firewalld.service
#禁止防火墙服务器,保证重新启动服务器之后防火墙还是关闭状态
systemctl disable firewalld.service

#安装vim命令
yum install -y vim

vim /etc/selinux/config
SELINUX=disabled
保存后重启: init 6

解压缩

cd /home/sys
tar -xvf mysql-5.7.35-1.el7.x86_64.rpm-bundle.tar

安装Server

cd /home/sys
rpm -ivh /home/sys/mysql-community-common-5.7.35-1.el7.x86_64.rpm
rpm -ivh /home/sys/mysql-community-libs-5.7.35-1.el7.x86_64.rpm
rpm -ivh /home/sys/mysql-community-client-5.7.35-1.el7.x86_64.rpm
rpm -ivh /home/sys/mysql-community-server-5.7.35-1.el7.x86_64.rpm

启动/关闭服务的命令是 systemctl start|stop

systemctl start mysqld

用 systemctl status 查看 MySQL 状态

systemctl status mysqld

设置开机启动

systemctl enable mysqld

重载所有修改过的配置文件

systemctl daemon-reload

mysql 安装完成之后,生成的默认密码在 /var/log/mysqld.log 文件中。使用 grep 命令找到日志中的密码。

grep 'temporary password' /var/log/mysqld.log

初次通过初始密码登录后,使用以下命令修改密码

mysql -u root -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyPassword'; 

添加一个允许远程连接的帐户

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'MyPassword' WITH GRANT OPTION;

刷新权限

FLUSH PRIVILEGES;

设置开机启动

systemctl enable mysqld

重载所有修改过的配置文件

systemctl daemon-reload

重启MySQL

systemctl restart mysqld

查看MySQL 状态

systemctl status mysqld

连接上MySQL 查看

在这里插入图片描述

恭喜 O(∩_∩)O ,MySQL5.7.35 安装成功

三、CentOS7 更改 MySQL5.7 数据目录位置

home目录下建立data目录

mkdir -p /home/data & cd /home/data/

把MySQL服务进程停掉

mysqladmin -u root -p shutdown
Enter password:

把/var/lib/mysql整个目录移到/home/data

mv /var/lib/mysql /home/data/
或者
cp -R /var/lib/mysql /home/data/  【本人当时用的这一个】

这样就把MySQL的数据文件移动到了/home/data/mysql下

设置/home/data/下mysql文件夹的属主和权限

chown -R mysql:mysql /home/data/mysql
ls
ll
chown -R mysql:mysql /home/data/mysql
ll

修改配置文件/etc/my.cnf

vim /etc/my.cnf

调整如下:
datadir=/home/data/mysql
socket=/home/data/mysql/mysql.sock

在my.cnf 这个文件顶部 添加如下配置,
否则可能会报错(Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.soc):

[client]
default-character-set=utf8
socket=/home/data/mysql/mysql.sock

配置完成my.cnf 如图:
在这里插入图片描述

重新启动MySQL服务

service mysqld start

用 systemctl status 查看 MySQL 状态

systemctl status mysqld

设置开机启动

systemctl enable mysqld

重载所有修改过的配置文件

systemctl daemon-reload

附录 - 卸载干净MySQL

查看mysql安装了哪些东西

rpm -qa |grep -i mysql


mysql57-community-release-el7-9.noarch
mysql-community-common-5.7.35-1.el7.x86_64
mysql-community-client-5.7.35-1.el7.x86_64
mysql-community-server-5.7.35-1.el7.x86_64
mysql-community-libs-5.7.35-1.el7.x86_64
mysql-community-libs-compat-5.7.35-1.el7.x86_64

开始卸载

yum remove 	 mysql57-community-release-el7-9.noarch
yum remove 	 mysql-community-common-5.7.35-1.el7.x86_64
yum remove 	 mysql-community-client-5.7.35-1.el7.x86_64
yum remove 	 mysql-community-server-5.7.35-1.el7.x86_64
yum remove 	 mysql-community-libs-5.7.35-1.el7.x86_64
yum remove 	 mysql-community-libs-compat-5.7.35-1.el7.x86_64

查看是否卸载完成

rpm -qa |grep -i mysql

查找mysql相关目录

find / -name mysql


/home/scdbdata/mysql
/home/bak/mysql
/home/bak/mysql/mysql
/etc/selinux/targeted/active/modules/100/mysql
/var/lib/mysql
/var/lib/mysql/mysql
/usr/share/mysql

删除相关目录

rm -rf /etc/selinux/targeted/active/modules/100/mysql
rm -rf /var/lib/mysql
rm -rf /var/lib/mysql/mysql
rm -rf /usr/share/mysql

删除/etc/my.cnf

rm -rf /etc/my.cnf

删除/var/log/mysqld.log(如果不删除这个文件,会导致新安装的mysql无法生存新密码,导致无法登陆)

rm -rf /var/log/mysqld.log

再次确认查看mysql安装了哪些东西

rpm -qa |grep -i mysql
没有东西,则表示卸载干净 O(∩_∩)O

附录 - 遇到的错误集锦

1.更换data目录之后报错:

Can’t connect to local MySQL server through socket '/var/lib/mysql/mysql.soc

解决方案: 在/etc/my.cnf 中添加如下配置:
[client]
default-character-set=utf8
socket=/home/data/mysql/mysql.sock

2.依赖检测失败:mariadb-libs is obsoleted by mysql-community-libs-8.0.20-1.el7.x86_64

执行一个命令:
yum remove mysql-libs
解决

3.linux安装mysql时报的错,这句话意思是需要 net-tools

yum -y install net-tools
解决!

报错信息[ERROR] InnoDB: Operating system error number 13 in a file operation.

解决方法: /etc/sysconfig/selinux
SELINUX=disabled

error: Failed dependencies:

    libaio.so.1()(64bit) is needed by mysql-community-server-5.7.35-1.el7.x86_64
    libaio.so.1(LIBAIO_0.1)(64bit) is needed by mysql-community-server-5.7.35-1.el7.x86_64
    libaio.so.1(LIBAIO_0.4)(64bit) is needed by mysql-community-server-5.7.35-1.el7.x86_64
    解决方法:yum install -y libaio

4. MySQL安装完毕 ,正常运行 没有初始密码的解决办法

	https://www.cnblogs.com/yanyanqaq/p/12801710.html
-- 设置密码命令
update user set authentication_string=password('你的密码') where user='root' and host='localhost';
-- 刷新权限
flush privileges;
;