CentOS 7 安装 MySQL 8

2,059 阅读7分钟

(图片来自 linux4one.com)

MySQL是一个开源的数据库管理系统,通常作为流行的LEMP(Linux,Nginx,MySQL/MariaDB,PHP/Python/Perl)堆栈安装,它使用关系数据库和SQL结构化语言。

如果你在CentOS 7上运行 yum install mysql,那就是安装了 MariaDB ,而不是MySQL。如果你想了解 MySQL 和 MariaDB 对比,可参考其它相关文档了解,MariaDB 通常可以无缝地代替 MySQL 。这篇文章只介绍在CentOS 7系统下安装 MySQL 8。

这一节咱们讲下怎样安装 MySQL ,网上的教程很多,这里会比较精简,一步步带领大家做完。

如果仅仅为了学习,可以选择试用云服务器或者在本地Docker环境上操作。以下操作均是在阿里云环境下验证。

预备知识

要开始这篇文章,你需要有以下预备知识:

  • 一个CentOS 7的系统,并且具有安装软件的权限
  • 基本的Linux命令

第一步 安装MySQL

前面提到过,默认情况下使用yum将安装MariaDB,要安装MySQL,我们需要访问MySQL社区提供的Yum资源包。

在浏览器上访问:

https://dev.mysql.com/downloads/repo/yum/

下载:

[root@iZwz998h7lw46btiezg4unZ ~]# wget https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm

下载完成后,可以与网站上对比一下md5sum,保证下载文件的完整性。

安装这个包:

[root@iZwz998h7lw46btiezg4unZ ~]# rpm -ivh mysql80-community-release-el7-3.noarch.rpm 
warning: mysql80-community-release-el7-3.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql80-community-release-el7-3  ################################# [100%]

这将添加两个新的MySQL源,接下来就可以使用它去安装MySQL服务了。

[root@iZwz998h7lw46btiezg4unZ ~]# yum install -y mysql-server

第二步 启动MySQL

使用下面的命令启动守护进程:

[root@iZwz998h7lw46btiezg4unZ ~]# systemctl start mysqld

如果没有任何输出就表示启动成功了。可以使用下面命令检查运行状态:

[root@iZwz998h7lw46btiezg4unZ ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2019-09-30 14:56:18 CST; 11s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 21657 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 21741 (mysqld)
   Status: "Server is operational"
   CGroup: /system.slice/mysqld.service
           └─21741 /usr/sbin/mysqld

Sep 30 14:56:10 iZwz998h7lw46btiezg4unZ systemd[1]: Starting MySQL Server...
Sep 30 14:56:18 iZwz998h7lw46btiezg4unZ systemd[1]: Started MySQL Server.

注:MySQL安装后会自动开机启动,可以执行systemctl disable mysqld改变这种默认行为。

安装完成后,会为root用户生成一个默认的密码:

[root@iZwz998h7lw46btiezg4unZ ~]# grep 'temporary password' /var/log/mysqld.log 
2019-09-30T06:56:14.710707Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 3So#w4,iVJr!

这个密码在下一步的安装配置中将会用到,并且会让你强制修改。

第三步 配置MySQL

MySQL包含一个脚本,可以简单配置一些不太安全的默认选项,比如远程root登录。

[root@iZwz998h7lw46btiezg4unZ ~]# mysql_secure_installation

将会提示你输入root用户的密码,也就是前面自动生成的默认密码。然后会需要修改密码。

注:默认的密码安全策略:至少包含一个大写字母,一个小写字母,一个数字,一个特殊字符。

然后可能会有一些提示问你是否要删除匿名用户、是否允许root用户远程登录,删除测试数据库等,建议这些都关掉,后面需要的时候手动配置。

[root@iZwz998h7lw46btiezg4unZ ~]# mysql_secure_installation 

Securing the MySQL server deployment.

Enter password for user root: 

The existing password for the user account root has expired. Please set a new password.

New password: 

Re-enter new password: 

Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 

Ok,到这里就已经安装完了,并且一些基本的配置也搞定了。

第四步 测试MySQL

[root@VM_64_70_centos ~]# mysqladmin -u root -p version

输入root用户密码后,将看到类似这样的输出:

[root@iZwz998h7lw46btiezg4unZ ~]# mysqladmin -u root -p version
Enter password: 
mysqladmin  Ver 8.0.17 for Linux on x86_64 (MySQL Community Server - GPL)
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.

Server version          8.0.17
Protocol version        10
Connection              Localhost via UNIX socket
UNIX socket             /var/lib/mysql/mysql.sock
Uptime:                 7 min 45 sec

Threads: 2  Questions: 15  Slow queries: 0  Opens: 131  Flush tables: 3  Open tables: 48  Queries per second avg: 0.032

表明已经安装成功。

第五步 开启远程连接

如果在第三步执行配置脚本的时候后面选择了不开启远程登录,那么需要手动配置开启远程登录。

编译MySQL配置文件:

[root@VM_64_70_centos ~]# vim /etc/my.cnf

把下面的配置粘贴到[mysqld]块的最下面

bind-address=*

重启MySQL

[root@VM_64_70_centos ~]# systemctl restart mysqld

下面为远程连接登录一个新用户,并且授予所有的特权: 先连接MySQL:

[root@iZwz998h7lw46btiezg4unZ ~]# mysql -u root -p
mysql> CREATE USER 'testuser'@'%' IDENTIFIED BY 'Testuser2019!';
mysql> GRANT ALL PRIVILEGES ON testdb.* TO 'testuser'@'%' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;

扩展

要向特定用户授予权限,可以使用此范式:

GRANT [type of permission] ON [database name].[table name] TO ‘[username]’@'localhost’;

如果要向其授予对任何数据库或任何表的访问权限,请确保在数据库名称或表名称的地方放置星号 *。

如果您需要撤消权限,其范式结构与授予的结构几乎相同:

REVOKE [type of permission] ON [database name].[table name] FROM ‘[username]’@‘localhost’;

如果需要修改用户密码:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你的密码';

如果需要删除用户:

DROP USER ‘demo’@‘localhost’;

用户信息均存储在 mysql.user 表里面:

mysql> select user,host from user;
+---------------+-----------+
| user          | host      |
+---------------+-----------+
| testdb        | %         |
| mysql.session | localhost |
| mysql.sys     | localhost |
| root          | localhost |
+---------------+-----------+
4 rows in set (0.00 sec)

可以看到对应的账户已经创建成功。

测试一下:

root@cbd5113a9d20:/# mysql -h 47.107.55.xx -P 3306 -u testuser -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 8.0.17 MySQL Community Server - GPL

Copyright (c) 2000, 2017, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| testdb             |
+--------------------+
2 rows in set (0.01 sec)

mysql>

Ok,可以看到已经远程登录成功了。(如果无法登录,请查看防火墙相关设置)

如果使用命令行可以连接MySQL,但使用类似 Navicat 客户端连接时出现如下错误:

2059 - Authentication plugin 'caching_sha2_password' cannot be loaded: dlopen(../Frameworks/caching_sha2_password.so, 2): image not found

是由于新版本的MySQL(我这里使用MySQL8)使用的是caching_sha2_password验证方式,但navicat还不支持。

解决办法就是将验证方式改为之前旧版本(5.7及以下)的mysql_native_password

ALTER USER 'testuser'@'*' IDENTIFIED WITH mysql_native_password BY 'Testuser2019!';

,具体的验证方式可以查看默认数据库'mysql'中user表plugin字段:

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

修改完后记得FLUSH PRIVILEGES;刷新使其立即生效。


相关阅读:

教你正确初始化 CentOS 7

CentOS 7 安装 Oracle JDK 8


欢迎关注公众号:非著名开发者,获取更多精彩内容。

ok_developer