3

本文介绍在centos-7下安装、卸载mysql 8.0,并使用SQLyog客户端工具连接mysql.

1. 卸载MySQL

1.1 卸载mariadb

检查mariadb: $ rpm -qa|grep mariadb

如果上面检查的结果有内容,则删除mariadb,把命令中的mariadb-server替换成所有上面输出的内容: $ rpm \-e --nodeps mariadb-server

1.2 卸载mysql

检查mysql:

$ rpm -qa|grep mysql
mysql80-community-release-el7-3.noarch
mysql-community-libs-8.0.19-1.el7.x86_64
mysql-community-server-8.0.19-1.el7.x86_64
mysql-community-common-8.0.19-1.el7.x86_64
mysql-community-client-8.0.19-1.el7.x86_64
$ systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2020-04-04 15:46:48 CST; 6 days ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
 Main PID: 9608 (mysqld)
   Status: "Server is operational"
   CGroup: /system.slice/mysqld.service
           └─9608 /usr/sbin/mysqld

停止mysqld.service服务:$ sudo systemctl stop mysqld

删除mysql程序:

$ sudo rpm -e --nodeps mysql80-community-release-el7-3.noarch mysql-community-libs-8.0.19-1.el7.x86_64 mysql-community-server-8.0.19-1.el7.x86_64 mysql-community-common-8.0.19-1.el7.x86_64 mysql-community-client-8.0.19-1.el7.x86_64
warning: /etc/my.cnf saved as /etc/my.cnf.rpmsave

删除所有mysql相关文件:$ sudo rm -rf /var/lib/mysql /var/log/mysqld.log

2. 安装MySQL

在CentOS-7中安装MySQL需要配置下载源,不能直接使用yum安装

2.1 添加MySQL Yum Repository

下载MySQL源

官网地址:dev.mysql.com/downloads

查看系统版本:

shell> cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)

选择对应的版本进行下载,例如CentOS 7当前在官网查看最新Yum源的下载地址为: dev.mysql.com/get/mysql80…

shell> wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
安装MySQL源
shell> sudo rpm -Uvh platform-and-version-specific-package-name.rpm

例如CentOS7当前最新MySQL源安装: shell> sudo rpm -Uvh mysql80-community-release-el7-3.noarch.rpm

检查是否安装成功
执行成功后会在/etc/yum.repos.d/目录下生成两个repo文件mysql-community.repo及 mysql-community-source.repo
并且通过yum repolist可以看到mysql相关资源

shell> yum repolist enabled | grep "mysql.*-community.*"
!mysql-connectors-community/x86_64 MySQL Connectors Community                108
!mysql-tools-community/x86_64      MySQL Tools Community                      90
!mysql80-community/x86_64          MySQL 8.0 Community Server                113

2.2 选择MySQL版本

使用MySQL Yum Repository安装MySQL,默认会选择当前最新的稳定版本,例如通过上面的MySQL源进行安装的话,默安装会选择MySQL 8.0版本,如果就是想要安装该版本,可以直接跳过此步骤,如果不是,比如我这里希望安装MySQL5.7版本,就需要“切换一下版本”:

查看当前MySQL Yum Repository中所有MySQL版本(每个版本在不同的子仓库中)
shell> yum repolist all | grep mysql
切换版本
shell> sudo yum-config-manager --disable mysql80-community
shell> sudo yum-config-manager --enable mysql57-community

2.3 安装MySQL

安装:$ sudo yum install mysql-community-server

启动:$ sudo systemctl start mysqld

查看状态:$ sudo systemctl status mysqld

2.4 设置root密码

我们自己进行测试时使用的root密码不必太复杂,通过设置my.cnf,可以设置简单密码(对mysql 8.0适用),设置完这些策略之后,必须执行sudo systemctl restart mysqld,才可使之生效:

validate_password.check_user_name = 0
validate_password.policy = 0
validate_password.mixed_case_count = 0
validate_password.number_count = 0
validate_password.special_char_count = 0
validate_password.length = 0

查看mysql root用户的密码:

$ sudo grep 'temporary password' /var/log/mysqld.log
2020-04-12T12:06:54.577571Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: d%tcKY,VO6:o

登录mysql:

$ mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.19

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 'root';
Query OK, 0 rows affected (0.01 sec)

查看密码策略:

mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+-------+
| Variable_name                        | Value |
+--------------------------------------+-------+
| validate_password.check_user_name    | OFF   |
| validate_password.dictionary_file    |       |
| validate_password.length             | 0     |
| validate_password.mixed_case_count   | 0     |
| validate_password.number_count       | 0     |
| validate_password.policy             | LOW   |
| validate_password.special_char_count | 0     |
+--------------------------------------+-------+
7 rows in set (0.00 sec)

2.5 允许远程连接

设置权限,允许远程连接root用户

mysql> CREATE USER 'root'@'%' IDENTIFIED BY 'root';
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)

设置加密方式,是SQLyog可以链接

mysql>  ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';
Query OK, 0 rows affected (0.00 sec)

2.6 打开3306端口

$ sudo firewall-cmd --zone=public --add-port=3306/tcp --permanent
[sudo] password for eric: 
success
$ sudo firewall-cmd --list-ports
$ sudo firewall-cmd --reload
success
$ sudo firewall-cmd --list-ports
3306/tcp

3. 客户端SQLyog连接MySQL

使用SQLyog连接mysql即可

参考文档


槲栎
67 声望1 粉丝