Last Updated on

前言

Mysql大家都懂的了,我就不过多做介绍了,直接进行mysql的安装吧,下面记录如果通过yum安装mysql5.7,此方法照样适用于安装8.0,5.6,5.5等mysql版本

正文

1. 删除mariadb

centos7 默认会安装mysql的另一个分支版本mariadb,在安装mysql前,需要将其删除

# 查出mariadb的rpm包
$ rpm -qa|grep mariadb
mariadb-libs-5.5.44-2.el7.centos.x86_64

# 忽略依赖关系,删除此包
$ rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64

2. 添加官方yum源并安装mysql

访问mysql官方yum源下载地址,选择系统,点击下载,进入页面,复制下载连接

# 下载安装此yum仓库
$ rpm -ivh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

# 安装完成后,此yum源仓库默认是安装mysql最新版8.0版本。
# 需要我们修改一下文件中的下列项,使其下载5.7版本,如果要安装别的版本,就打开对应版本,将别的版本关闭即可。
$ vim /etc/yum.repos.d/mysql-community.repo
----------------------------------------------------------------
# Enable to use MySQL 5.7
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1    # 这里将0改为1,开启5.7版本
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
enabled=0    # 这里将1改为0,关闭8.0版本
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
----------------------------------------------------------------

# 修改完成后,安装mysql5.7
$ yum install -y mysql-community-server

安装完成后,配置文件为 /etc/my.cnf 根据实际情况进行配置。

3. 启动服务,开启远程访问

# 启动mysql服务
$ systemctl start mysqld

# 设为开启自启动
$ systemctl enable mysqld

# 第一次启动mysql后,会自动生成一个初始密码在日志文件中,默认的日志文件路径为:/var/log/mysqld.log
# 找出初始化密码
$ cat /var/log/mysqld.log |grep password
2019-06-18T12:12:04.161088Z 1 [Note] A temporary password is generated for root@localhost: 2%w)IDysCVZQ

# 这里密码就是上面输出的那段,使用此密码登录mysql
$ mysql -u root -p 
# 复制密码输出,进入mysql
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.26

Copyright (c) 2000, 2019, 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> 

# 修改密码,mysql5.7的默认密码强度很高,需要有大小写,符号和数字,长度大于6,缺一不可
mysql> alter user 'root'@'localhost' identified by 'your_password';
Query OK, 0 rows affected (0.00 sec)

# 如果你不像要这么复杂的密码,修改密码强度设定为最低,即只判断密码长度
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)

# 开启远程访问
$ grant all privileges on *.* to 'root'@'%' identified by 'your_password';
Query OK, 0 rows affected, 1 warning (0.00 sec)

# 刷新权限生效
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)



# 到此就完成了mysql的最初设置了,现在就可以用账号密码,访问此mysql服务了
# 访问之前别忘了要打开数据库3306端口
$ firewall-cmd --add-port=3306/tcp --permanent
$ firewall-cmd --reload

结束

ok,就这么简单的就完成了mysql5.7的yum安装,通过此方法同样可以安装mysql5.5,5.6,8.0这些版本。只要官方的yum仓库支持就行,非常简单方便。

有什么问题,欢迎留言