CentOS 7 - 安装 MySQL 8.0

12 篇文章 0 订阅

CentOS 7 - 安装 MySQL 8.0

提示:以下操作均以 root 用户执行。

环境信息

操作系统版本:

# rpm -q centos-release 
centos-release-7-7.1908.0.el7.centos.x86_64

删除 mariadb 数据库

CentOS 7 默认安装了 mariadb,我们可以先将其删除:

# rpm -qa | grep mariadb // 查询是否已安装的 mariadb
mariadb-libs-5.5.64-1.el7.x86_64
# rpm -e --nodeps mariadb-libs-5.5.64-1.el7.x86_64 // 删除 mariadb

安装 MySQL

下载 MySQL Yum Repository

根据操作系统下载 MySQL Yum Repository,我的是 CentOS 7,所以选择第二个。此外,还需要记住 MD5,稍后可用于校验文件的完整性:

在这里插入图片描述

下载完成之后,进入文件所在目录。使用 md5sum 命令计算文件的 MD5,如果与 MySQL 提供的 MD5 一致,那就 OK:

# ll
total 28
-rw-r--r--. 1 root root 26024 Mar 10 20:36 mysql80-community-release-el7-3.noarch.rpm
# md5sum mysql80-community-release-el7-3.noarch.rpm 
893b55d5d885df5c4d4cf7c4f2f6c153  mysql80-community-release-el7-3.noarch.rpm

添加 MySQL Yum Repository

接着,添加 MySQL Yum 仓库到系统的仓库列表:

# yum localinstall mysql80-community-release-el7-3.noarch.rpm
Loaded plugins: fastestmirror, langpacks
Examining mysql80-community-release-el7-3.noarch.rpm: mysql80-community-release-el7-3.noarch
Marking mysql80-community-release-el7-3.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mysql80-community-release.noarch 0:el7-3 will be installed
--> Finished Dependency Resolution
base/7/x86_64                                                         | 3.6 kB  00:00:00     
extras/7/x86_64                                                       | 2.9 kB  00:00:00     
extras/7/x86_64/primary_db                                            | 164 kB  00:00:00     
updates/7/x86_64                                                      | 2.9 kB  00:00:00     
updates/7/x86_64/primary_db                                           | 6.7 MB  00:00:02     

Dependencies Resolved

=============================================================================================
 Package                    Arch    Version   Repository                                Size
=============================================================================================
Installing:
 mysql80-community-release  noarch  el7-3     /mysql80-community-release-el7-3.noarch   31 k

Transaction Summary
=============================================================================================
Install  1 Package

Total size: 31 k
Installed size: 31 k
Is this ok [y/d/N]: 

询问是否 OK,输入 y 继续:

Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
** Found 2 pre-existing rpmdb problem(s), 'yum check' output follows:
2:postfix-2.10.1-7.el7.x86_64 has missing requires of libmysqlclient.so.18()(64bit)
2:postfix-2.10.1-7.el7.x86_64 has missing requires of libmysqlclient.so.18(libmysqlclient_18)(64bit)
  Installing : mysql80-community-release-el7-3.noarch                                    1/1 
  Verifying  : mysql80-community-release-el7-3.noarch                                    1/1 

Installed:
  mysql80-community-release.noarch 0:el7-3                                                   

Complete!

使用以下命令检查 MySQL Yum 仓库是否添加成功:

# yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community/x86_64       MySQL Connectors Community           141
mysql-tools-community/x86_64            MySQL Tools Community                105
mysql80-community/x86_64                MySQL 8.0 Community Server           161

注意:Once the MySQL Yum repository is enabled on your system, any system-wide update by the yum update command (or dnf upgrade for dnf-enabled systems) will upgrade MySQL packages on your system and also replace any native third-party packages, if Yum finds replacements for them in the MySQL Yum repository; see Section 2.11.7, “Upgrading MySQL with the MySQL Yum Repository” and, for a discussion on some possible effects of that on your system, see Upgrading the Shared Client Libraries.

安装 MySQL

使用以下命令安装 MySQL:

# yum install mysql-community-server
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: ap.stykers.moe
 * extras: ap.stykers.moe
 * updates: ap.stykers.moe
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:8.0.19-1.el7 will be installed
--> Processing Dependency: mysql-community-common(x86-64) = 8.0.19-1.el7 for package: mysql-community-server-8.0.19-1.el7.x86_64
--> Processing Dependency: mysql-community-client(x86-64) >= 8.0.11 for package: mysql-community-server-8.0.19-1.el7.x86_64
--> Running transaction check
---> Package mysql-community-client.x86_64 0:8.0.19-1.el7 will be installed
--> Processing Dependency: mysql-community-libs(x86-64) >= 8.0.11 for package: mysql-community-client-8.0.19-1.el7.x86_64
---> Package mysql-community-common.x86_64 0:8.0.19-1.el7 will be installed
--> Running transaction check
---> Package mysql-community-libs.x86_64 0:8.0.19-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================
 Package                      Arch         Version             Repository               Size
=============================================================================================
Installing:
 mysql-community-server       x86_64       8.0.19-1.el7        mysql80-community       436 M
Installing for dependencies:
 mysql-community-client       x86_64       8.0.19-1.el7        mysql80-community        41 M
 mysql-community-common       x86_64       8.0.19-1.el7        mysql80-community       605 k
 mysql-community-libs         x86_64       8.0.19-1.el7        mysql80-community       4.0 M

Transaction Summary
=============================================================================================
Install  1 Package (+3 Dependent packages)

Total download size: 482 M
Installed size: 2.1 G
Is this ok [y/d/N]: 

输入 y 继续:

Is this ok [y/d/N]: y
Downloading packages:
warning: /var/cache/yum/x86_64/7/mysql80-community/packages/mysql-community-common-8.0.19-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Public key for mysql-community-common-8.0.19-1.el7.x86_64.rpm is not installed
(1/4): mysql-community-common-8.0.19-1.el7.x86_64.rpm                 | 605 kB  00:00:01     
(2/4): mysql-community-libs-8.0.19-1.el7.x86_64.rpm                   | 4.0 MB  00:00:08     
(3/4): mysql-community-client-8.0.19-1.el7.x86_64.rpm                 |  41 MB  00:13:31     
(4/4): mysql-community-server-8.0.19-1.el7.x86_64.rpm                 | 436 MB  00:17:35     
---------------------------------------------------------------------------------------------
Total                                                        463 kB/s | 482 MB  00:17:45     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Importing GPG key 0x5072E1F5:
 Userid     : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
 Fingerprint: a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
 Package    : mysql80-community-release-el7-3.noarch (@/mysql80-community-release-el7-3.noarch)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Is this ok [y/N]: 

输入 y 继续:

Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mysql-community-common-8.0.19-1.el7.x86_64                                1/4 
  Installing : mysql-community-libs-8.0.19-1.el7.x86_64                                  2/4 
  Installing : mysql-community-client-8.0.19-1.el7.x86_64                                3/4 
  Installing : mysql-community-server-8.0.19-1.el7.x86_64                                4/4 
  Verifying  : mysql-community-server-8.0.19-1.el7.x86_64                                1/4 
  Verifying  : mysql-community-client-8.0.19-1.el7.x86_64                                2/4 
  Verifying  : mysql-community-libs-8.0.19-1.el7.x86_64                                  3/4 
  Verifying  : mysql-community-common-8.0.19-1.el7.x86_64                                4/4 

Installed:
  mysql-community-server.x86_64 0:8.0.19-1.el7                                               

Dependency Installed:
  mysql-community-client.x86_64 0:8.0.19-1.el7  mysql-community-common.x86_64 0:8.0.19-1.el7 
  mysql-community-libs.x86_64 0:8.0.19-1.el7   

Complete!

执行以上命令安装 MySQL 服务器(mysql-community-server)所需的包,以及运行服务器所需组件的包,包括客户端(mysql-community-client)、用于客户端和服务器的常见错误消息和字符集(mysql-community-common),共享客户端库(mysql-community-libs)。

启动 MySQL 服务器

检查 MySQL 服务器的状态:

# service mysqld status
\Redirecting to /bin/systemctl status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html

启动 MySQL 服务器:

# service mysqld start
Redirecting to /bin/systemctl start mysqld.service

启动之后,检查 MySQL 服务器的状态:

# service mysqld status
Redirecting to /bin/systemctl status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2020-03-10 21:47:07 CST; 18s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 8688 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 8773 (mysqld)
   Status: "Server is operational"
    Tasks: 39
   CGroup: /system.slice/mysqld.service
           └─8773 /usr/sbin/mysqld

Mar 10 21:46:38 192.168.88.144 systemd[1]: Starting MySQL Server...
Mar 10 21:47:07 192.168.88.144 systemd[1]: Started MySQL Server.

第一次启动 MySQL 服务器之后,默认创建一个超级账户 'root'@'localhost',其密码存储在错误日志文件中。可以通过以下命令查看:

# grep 'temporary password' /var/log/mysqld.log
2020-03-10T13:46:52.205710Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: Rglql7o6na!/

使用 MySQL 客户端连接 MySQL 服务器:

# mysql -u root -p
Enter password: Rglql7o6na!/
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> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';
Query OK, 0 rows affected (0.01 sec)

注意validate_password is installed by default. The default password policy implemented by validate_password requires that passwords contain at least one uppercase letter, one lowercase letter, one digit, and one special character, and that the total password length is at least 8 characters.

修改字符集

连接 MySQL 服务器之后,可以查看默认的字符集(第 15 - 18 行):

mysql> status
--------------
mysql  Ver 8.0.19 for Linux on x86_64 (MySQL Community Server - GPL)

Connection id:		11
Current database:	mysql
Current user:		root@localhost
SSL:			Not in use
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server version:		8.0.19 MySQL Community Server - GPL
Protocol version:	10
Connection:		Localhost via UNIX socket
Server characterset:	utf8mb4
Db     characterset:	utf8mb4
Client characterset:	utf8mb4
Conn.  characterset:	utf8mb4
UNIX socket:		/var/lib/mysql/mysql.sock
Binary data as:		Hexadecimal
Uptime:			1 hour 17 min 13 sec

Threads: 2  Questions: 59  Slow queries: 0  Opens: 183  Flush tables: 3  Open tables: 100  Queries per second avg: 0.012
--------------

可以通过修改 /etc/my.cnf 文件更改字符集:

[mysqld]
# 其他配置,略

character-set-server=utf8
collation-server=utf8_general_ci

[client]
default-character-set=utf8

修改完成之后,重启 MySQL 服务器,然后查看状态:

mysql> status
--------------
mysql  Ver 8.0.19 for Linux on x86_64 (MySQL Community Server - GPL)

Connection id:		8
Current database:	
Current user:		root@localhost
SSL:			Not in use
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server version:		8.0.19 MySQL Community Server - GPL
Protocol version:	10
Connection:		Localhost via UNIX socket
Server characterset:	utf8
Db     characterset:	utf8
Client characterset:	utf8
Conn.  characterset:	utf8
UNIX socket:		/var/lib/mysql/mysql.sock
Binary data as:		Hexadecimal
Uptime:			38 sec

Threads: 2  Questions: 6  Slow queries: 0  Opens: 115  Flush tables: 3  Open tables: 35  Queries per second avg: 0.157
--------------

允许远程主机连接本地服务器

新建一个账户,允许 192.168.1.% 网段的客户端连接到本机,% 是一个通配符:

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

嗯,网段搞错了,修改一下:

mysql> RENAME USER 'root'@'192.168.1.%' TO 'root'@'192.168.88.%';
Query OK, 0 rows affected (0.01 sec)

注意:如果要兼容 MySQL 5.7 客户端的连接,则需要指定认证插件 mysql_native_password:

mysql> ALTER USER 'root'@'192.168.88.%' IDENTIFIED WITH mysql_native_password BY 'MyNewPass4!';
Query OK, 0 rows affected (0.01 sec)

新建的账户被添加到 mysql 数据库的 user 表中:

mysql> SELECT host, user, plugin FROM mysql.user;
+--------------+------------------+-----------------------+
| host         | user             | plugin                |
+--------------+------------------+-----------------------+
| 192.168.88.% | root             | mysql_native_password |
| localhost    | mysql.infoschema | caching_sha2_password |
| localhost    | mysql.session    | caching_sha2_password |
| localhost    | mysql.sys        | caching_sha2_password |
| localhost    | root             | caching_sha2_password |
+--------------+------------------+-----------------------+
5 rows in set (0.00 sec)

分配权限给该账户:

mysql> GRANT ALL // 所有权限
    -> ON *.* // 任意数据库的任意表
    -> TO 'root'@'192.168.88.%'; // 给那一个账户
Query OK, 0 rows affected (0.00 sec)

注意:以上提供的方法是不安全的,均供学习使用。

开放 3306 端口

启动防火墙

查看防火墙状态:

# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2020-03-10 16:36:08 CST; 5h 43min ago
     Docs: man:firewalld(1)
 Main PID: 791 (firewalld)
    Tasks: 2
   CGroup: /system.slice/firewalld.service
           └─791 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid

Mar 10 16:36:06 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall .....
Mar 10 16:36:08 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall d...n.
Hint: Some lines were ellipsized, use -l to show in full.

如果没有启动(active),可以使用以下命令启动防火墙:

# systemctl start firewalld

添加 3306 端口

查看当前激活的 zone:

# firewall-cmd --get-active-zones
public
  interfaces: ens33

添加端口:

# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success

重启防火墙:

# firewall-cmd --reload
success

查看端口是否开启:

# firewall-cmd --query-port=3306/tcp
yes

远程主机连接测试

假设以上提到的 MySQL 安装主机 B,既 CentOS 7。

在主机 A 使用 MySQL 5.7 客户端连接到主机 B 的 MySQL 8.0 服务器,其中 192.168.88.144 是主机 B 的 IP:

C:\Users\86188>mysql --default-character-set=utf8 -h 192.168.88.144 -u root -p
Enter password: ***********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.19 MySQL Community Server - GPL

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

查看状态,第 7 行显示当前客户端的地址:

mysql> status
--------------
mysql  Ver 14.14 Distrib 5.7.17, for Win64 (x86_64)

Connection id:          12
Current database:
Current user:           root@192.168.88.1
SSL:                    Cipher in use is DHE-RSA-AES256-SHA
Using delimiter:        ;
Server version:         8.0.19 MySQL Community Server - GPL
Protocol version:       10
Connection:             192.168.88.144 via TCP/IP
Server characterset:    utf8
Db     characterset:    utf8
Client characterset:    utf8
Conn.  characterset:    utf8
TCP port:               3306
Uptime:                 21 min 48 sec

Threads: 3  Questions: 11  Slow queries: 0  Opens: 125  Flush tables: 3  Open tables: 45  Queries per second avg: 0.008
--------------

注意:如果在主机 A 使用 MySQL 5.7 客户端连接主机 B 的 MySQL 8.0 服务器,且主机 B 的 MySQL 8.0 服务器在创建一个允许远程主机连接的用户时,没有指定兼容 5.7 版本的认证插件,那么,你将看到如下类似错误信息:

mysql --default-character-set=utf8 -h 192.168.88.144 -u root -p
Enter password: ***********
ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded: ... 提示信息

参考

Installing MySQL on Linux Using the MySQL Yum Repository

Collation Naming Conventions

Character Sets and Collations in MySQL

Character Set Configuration

CREATE USER Statement

RENAME USER Statement

GRANT Statement

MySQL 8 允许远程主机连接本地服务器

MySQL 8.0报错:error 2059: Authentication plugin ‘caching_sha2_password’ cannot be loaded

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值