centos7安装mysql5.7

Posted zk1023

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了centos7安装mysql5.7相关的知识,希望对你有一定的参考价值。

1.先删除系统原有的mysql

# 查看yum是否安装过mysql

yum list installed mysql*

如或显示了列表,说明系统中有MySQL

yum remove mysql-community-client mysql-community-common mysql-community-libs mysql-community-libs-compat mysql-community-server mysql57-community-release

rm -rf /var/lib/mysql

rm /etc/my.cnf

执行 rpm -qa|grep mariadb 查看是否有mariadb的安装包

有则执行 例如 rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64  删除上面语句查出来的包

rpm -qa | grep -i  mysql 查看是否有mysql 的安装包

有则删除安装包

cd /var/lib/

rm -rf mysql/

清除余项

whereis mysql

mysql: /usr/bin/mysql /usr/lib64/mysql /usr/local/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz

# 删除上面的文件夹

rm -rf /usr/bin/mysql 等。

删除配置

rm –rf /usr/my.cnf

rm -rf /root/.mysql_sercret

chkconfig --list | grep -i mysql

chkconfig --del mysqld

 

2. https://dev.mysql.com/downloads/repo/yum/   在MySQL官网下载5.7

 

复制链接

 

 

3.进行安装

cd /usr/local/src/

wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

rpm -ivh mysql80-community-release-el7-3.noarch.rpm

 

查看yum源中的mysql安装包

 yum repolist all | grep mysql   

 

上面的命令可以看到yum源中默认启用的安装包版本为MySQL8.0,如果需要切换为5.7,需要运行以下命令;

yum-config-manager --disable mysql80-community

yum-config-manager --enable mysql57-community

 

yum repolist all | grep mysql

yum -y install mysql-community-server   --执行太慢失败

 

由于yum下载太慢 参考https://www.cnblogs.com/zk1023/p/12303775.html  还是不行 --执行太慢失败

所以用离线方式下载

yum 下载的缓存文件路径在 :

[root@zsls src]# cd /var/cache/yum/x86_64/7/mysql57-community/packages/

[root@zsls packages]# ls

mysql-community-client-5.7.29-1.el7.x86_64.rpm  mysql-community-libs-5.7.29-1.el7.x86_64.rpm

mysql-community-common-5.7.29-1.el7.x86_64.rpm  mysql-community-server-5.7.29-1.el7.x86_64.rpm

 

国内镜像站:

http://uni.mirrors.163.com/mysql/Downloads/

http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/

从上面国内镜像下载对应的rpm文件  在通过sftp上传到服务器

然后在执行    yum -y install mysql-community-server 

 

4.启动mysql服务 

systemctl start mysqld.service

查看mysql服务运行状态

systemctl status mysqld.service

 

[root@zsls packages]# systemctl start mysqld.service
[root@zsls packages]# systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2020-02-13 17:34:38 CST; 3min 48s ago

 

获取安装时临时密码

[root@zsls packages]# grep \'temporary password\' /var/log/mysqld.log
2020-02-13T09:34:34.619238Z 1 [Note] A temporary password is generated for root@localhost: aAbffS1(5)o;

通过使用生成的临时密码登录并为超级用户帐户设置自定义密码,尽快更改root密码

[root@zsls packages]# mysql -uroot -p
Enter password:

输入以上临时密码

 

修改密码

alter user \'root\'@\'localhost\' identified by \'zsls@123\';

如果您需要修改简单密码,可以依次执行以下操作步骤:

set global validate_password_policy=0;

set global validate_password_length=1;

set global validate_password_mixed_case_count=2;

alter user \'root\'@\'localhost\' identified by \'123456\';

 

授权其他机器可以登录

GRANT ALL PRIVILEGES ON *.* TO \'root\'@\'%\' IDENTIFIED BY \'123456\' WITH GRANT OPTION;

FLUSH  PRIVILEGES;

 

配置默认编码为utf8:

vi /etc/my.cnf

#添加

[mysqld]

character_set_server=utf8mb4

init_connect=\'SET NAMES utf8mb4\'

其他默认配置文件路径:

配置文件:/etc/my.cnf

日志文件:/var/log/mysqld.log

服务启动脚本:/usr/lib/systemd/system/mysqld.service

设置开机启动
systemctl enable mysqld
systemctl daemon-reload

chkconfig --list       显示服务列表

 

##********高可用*******##

mysql高可用架构可以通过 MHA  实现

 

5.备份数据

mysqldump -h192.16.4.67 -uroot -p123456 -P3306 --databases data_test|gzip > /root/mysqlbak/data_test_sql.gz

 

[root@app-DB-1 mysqlbak]# ls
data_test_sql.gz

 

导入:

mysql> use data_test;

mysql> source /app/rule-web/data_test_sql.sql;

 

6.安装遇到问题

在安装降低版本为mysql5.6 时  MySQL Daemon failed to start  

尝试先初始化mysql,输入命令:mysqld --initialize

[root@zsls]# mysqld --initialize
2020-08-19 11:42:01 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-08-19 11:42:01 0 [Note] mysqld (mysqld 5.6.49) starting as process 15592 ...
2020-08-19 11:42:01 15592 [ERROR] Fatal error: Please read "Security" section of the manual to find out how to run mysqld as root!

2020-08-19 11:42:01 15592 [ERROR] Aborting

2020-08-19 11:42:01 15592 [Note] Binlog end
2020-08-19 11:42:01 15592 [Note] mysqld: Shutdown complete

说明数据文件夹已被创建,直接删除,让mysql自己创建即可,输入命令:rm -rf /var/lib/mysql
删除后,注意:然后不需要再初始化了,直接启动mysql,它会自己初始化,输入命令:service mysqld start # 这里写的是centos6 启动方式  如果是centos7用7的启动方式出现下面的截图信息  可以按照截图信息进行设置密码等    

 

 

 

 

以上是关于centos7安装mysql5.7的主要内容,如果未能解决你的问题,请参考以下文章

MySql之centos7.2安装mysql5.7.13

centos7.2安装mysql5.7.13及ssl主从复制

centos7安装mysql5.7步骤(图解版)

centos7安装mysql5.7步骤(图解版)

CentOS7安装MYSQL5.7

CentOS7安装MySQL5.7操作说明