用C语言编写,MySQL是一个开源,跨平台以及最广泛使用的关系型数据库管理系统(RDMS)之一。它是LAMP栈不可分割的部分并且是网站托管,数据分析和电子商务中流行的数据库管理系统。
当前稳定发行是MySQL 8.0.25并且在2021年5月11日发布。最近发行的重点是:
- InnoDB & XML增强
- 事务数据字典
- 对本地JSON数据和文档存储功能的增加支持
- 公共表达式
- 窗口函数
- 诸如错误编号和减少冗余的错误日志增强
以及更多,对于所有特性和增强的综合涵盖你可以检查发布注释。
在本教程中,我们演示了如何在Rocky Linux和AlmaLinux上安装MySQL。
第一步:更新Rocky Linux
当我们安装软件包时,总是推荐从系统包更新开始。在某些情况下,升级内核时需要一次重启。因而,在命令行上,执行以下命令来更新内核和系统包:
[root@localhost dbuser]# dnf update -y
Last metadata expiration check: 0:13:15 ago on Tue 28 Jun 2022 04:05:16 PM CST.
Dependencies resolved.
=========================================================================================================================================================
Package Architecture Version Repository Size
=========================================================================================================================================================
Upgrading:
almalinux-release x86_64 8.6-2.el8
...
Installed:
grub2-tools-efi-1:2.02-123.el8_6.8.alma.x86_64
Complete!
[root@localhost dbuser]#
第二步:启用MySQL上游模块
继续前进,我们需要启用由AppStream仓库提供的MySQL 8.0模块。现在,这是被提供的唯一MySQL模块并且你能够通过运行以下命令验证这个:
[root@localhost dbuser]# dnf module list mysql
Last metadata expiration check: 0:28:50 ago on Tue 28 Jun 2022 04:05:16 PM CST.
AlmaLinux 8 - AppStream
Name Stream Profiles Summary
mysql 8.0 [d] client, server [d] MySQL Module
Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled
要启用MySQL模块srream,运行以下命令:
Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled
[root@localhost dbuser]# dnf module enable mysql:8.0
Last metadata expiration check: 0:30:05 ago on Tue 28 Jun 2022 04:05:16 PM CST.
Dependencies resolved.
=========================================================================================================================================================
Package Architecture Version Repository Size
=========================================================================================================================================================
Enabling module streams:
mysql 8.0
Transaction Summary
=========================================================================================================================================================
Is this ok [y/N]: y
Complete!
第三步:在Rocky Linux上安装MySQL 8.0
使能了这个模块,按如下在Rocky Linux上安装MySQL 8.0:
[root@localhost dbuser]# dnf install @mysql
Last metadata expiration check: 0:31:52 ago on Tue 28 Jun 2022 04:05:16 PM CST.
Dependencies resolved.
=========================================================================================================================================================
Package Architecture Version Repository Size
=========================================================================================================================================================
Installing group/module packages:
mysql-server x86_64 8.0.26-1.module_el8.4.0+2532+b8928c02 appstream 25 M
Installing dependencies:
mariadb-connector-c-config noarch 3.1.11-2.el8_3 appstream 14 k
mecab x86_64 0.996-1.module_el8.4.0+2532+b8928c02.9 appstream 392 k
mysql x86_64 8.0.26-1.module_el8.4.0+2532+b8928c02 appstream 12 M
mysql-common x86_64 8.0.26-1.module_el8.4.0+2532+b8928c02 appstream 133 k
mysql-errmsg x86_64 8.0.26-1.module_el8.4.0+2532+b8928c02 appstream 597 k
protobuf-lite x86_64 3.5.0-13.el8 appstream 148 k
Installing module profiles:
mysql/server
Transaction Summary
=========================================================================================================================================================
Install 7 Packages
Total download size: 38 M
Installed size: 195 M
Is this ok [y/N]: y
Downloading Packages:
...
Installed:
mariadb-connector-c-config-3.1.11-2.el8_3.noarch mecab-0.996-1.module_el8.4.0+2532+b8928c02.9.x86_64
mysql-8.0.26-1.module_el8.4.0+2532+b8928c02.x86_64 mysql-common-8.0.26-1.module_el8.4.0+2532+b8928c02.x86_64
mysql-errmsg-8.0.26-1.module_el8.4.0+2532+b8928c02.x86_64 mysql-server-8.0.26-1.module_el8.4.0+2532+b8928c02.x86_64
protobuf-lite-3.5.0-13.el8.x86_64
Complete!
第四步:使能并且启动MySQL
要用MySQL数据库服务器执行任何操作,我们需要首先启动这个服务。但首先,按以下使其能在系统启动时启动:
[root@localhost dbuser]# systemctl enable mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
接着启动MySQL守护程序:
[root@localhost dbuser]# systemctl start mysqld
我们通过执行以下能够验证MySQL正在运行:
[root@localhost dbuser]# systemctl status mysqld
● mysqld.service - MySQL 8.0 database server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2022-06-28 16:41:22 CST; 6s ago
Process: 34862 ExecStartPost=/usr/libexec/mysql-check-upgrade (code=exited, status=0/SUCCESS)
Process: 34735 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service (code=exited, status=0/SUCCESS)
Process: 34710 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)
Main PID: 34817 (mysqld)
Status: "Server is operational"
Tasks: 38 (limit: 23504)
Memory: 462.2M
CGroup: /system.slice/mysqld.service
└─34817 /usr/libexec/mysqld --basedir=/usr
Jun 28 16:41:14 localhost.localdomain systemd[1]: Starting MySQL 8.0 database server...
Jun 28 16:41:14 localhost.localdomain mysql-prepare-db-dir[34735]: Initializing MySQL database
Jun 28 16:41:22 localhost.localdomain systemd[1]: Started MySQL 8.0 database server.
第五步:在Rocky Linux中使MySQL安全
最后一步是要使得刚安装的MySQL数据库服务器安全。原因是:默认,MySQL带有充满漏洞的默认设置出现,这些漏洞会被黑客利用。像这样,我们需要通过运行mysql_secure_installation脚本加固它。
当执行这个脚本时,将提示你使用VALIDATE_PASSWORD插件,它确定了密码强度等级并且使得使用者只能设置健壮的密码。
设置这个插件,输入'Y'并且按回车。这个插件提供了3种密码策略等级,即是:LOW, MEDIUM和STRONG。
对应STRONG密码策略输入2并且按回车、
接着,确定设置一个符合所选密码等级的强MYSQL根密码。
[root@localhost dbuser]# mysql_secure_installation
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No: y
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
Please set the password for root here.
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) : n
... skipping.
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!
这个插件提供了一种密码强度的估计,在这种情况中,100。这表示我们满足了密码强度要求:一个强密码需要至少8个字符,其应该由数值,大小写和特殊字符组成。
要继续设置密码,按'Y'。否则,按'n'回退并且做所需调整。
对于接下来的提示,按'Y'移除匿名用户,防止root用户远程登陆,并且在进入到一个生产环境前,移除测试数据库。
[root@localhost dbuser]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.26 Source distribution
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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> SELECT_VERSION();
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT_VERSION()' at line 1
mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 8.0.26 |
+-----------+
1 row in set (0.00 sec)
mysql>
第六步:在Rocky Linux中连接到MySQL
一旦做完,使用MySQL客户端按演示地连接数据库。证实提供的root密码。
[dbuser@localhost ~]$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 8.0.26 Source distribution
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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>
你可以通过运行以下查询确认版本:
mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 8.0.26 |
+-----------+
1 row in set (0.00 sec)
mysql>
在Rocky Linux或AlmaLinux上安装MySQL是一个非常简单和明白的过程。由于AppStrem仓库已经提供MySQL8.0模块stream, 没有必要添加任何第三方仓库。