安装MySql

安装CentOS8默认软件源的MYSQL,设置开机启动,设置允许远程连接mysql

环境:Linux 的Centos 8.2
MySQL 社区下载

安装

在CentOS 8默认的软件源中提供了MySQL 8,所以我们可以直接安装。
运行以下命令,安装mysql-server软件包及其依赖项:

1
sudo dnf install mysql-server
终端演示 出现确认提示时,输入 y,然后ENTER确认以继续:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@VM-16-17-centos ~]# sudo dnf install mysql-server
上次元数据过期检查:1:59:23 前,执行于 2022年03月28日 星期一 09时44分03秒。
依赖关系解决。
...
安装 7 软件包
总下载:38 M
安装大小:195 M
确定吗?[y/N]: y
下载软件包:
...
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
...
已安装:
...
完毕!

完成之后,MySQL就已成功安装在我们的服务器上,但是仍处于未运行状态。我们刚安装的MySQL软件包,系统将其配置成名为mysqld.service的systemd服务。使用以下systemctl命令启动MySQL:

1
sudo systemctl start mysqld.service

要检查MySQL服务是否正常运行,运行以下命令查看mysqld服务的状态。虽然MySQL的服务名称是“mysqld.service”,但是我们在使用systemctl命令的时候,并不需要在mysqld后面带上.service:

1
sudo systemctl status mysqld

如果MySQL成功启动,则输出内容更将会显示MySQL服务处于“active(running)”状态:

终端演示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@VM-16-17-centos ~]# sudo systemctl status mysqld
● mysqld.service - MySQL 8.0 database server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2022-03-28 11:45:41 CST; 38s ago
Process: 20720 ExecStartPost=/usr/libexec/mysql-check-upgrade (code=exited, status=0/SUCCESS)
Process: 20572 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service (code=exited, status=0/SUCCESS)
Process: 20547 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)
Main PID: 20670 (mysqld)
Status: "Server is operational"
Tasks: 38 (limit: 11504)
Memory: 449.3M
CGroup: /system.slice/mysqld.service
└─20670 /usr/libexec/mysqld --basedir=/usr

3月 28 11:45:31 VM-16-17-centos systemd[1]: Starting MySQL 8.0 database server...
3月 28 11:45:31 VM-16-17-centos mysql-prepare-db-dir[20572]: Initializing MySQL database
3月 28 11:45:41 VM-16-17-centos systemd[1]: Started MySQL 8.0 database server.

将MySQL设置为随服务器启动而启动

1
sudo systemctl enable mysqld

不想设置MySQL开机启动,可以通过运行以下命令来设置
sudo systemctl disable mysqld

增强 MySQL 安全性

MySQL包含一个安全脚本,该脚本允许我们为MySQL设置密码。

运行以下命令:

1
sudo mysql_secure_installation

这将引导我们完成一系列操作,系统将询问是否要对MySQL安装的安全性选项进行更改。第一步将询问我们是否要设置验证密码。

按“Y”确认我们要设置密码,然后系统将要求我们选择密码验证级别。输入最高级级别2,该级别要求密码大于8个字符,并包括大写,小写,数字和特殊字符的组合:

终端演示
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@VM-16-17-centos ~]# sudo 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: 1

下一步,提示为MySQL 的root用户设置密码。 根据刚才选择的级别2的要求,输入相应的密码:

终端演示
1
2
3
4
5
6
7
8
Please set the password for root here.
New password:
Re-enter new password:
Estimated strength of the password: 50
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
... Failed! Error: Your password does not satisfy the current policy requirements
New password:
Re-enter new password:

如果我们输入的密码不符合级别2要求的等级,系统会提示重新按要求输入密码,密码应该大于8个字符,并包括大写,小写,数字和特殊字符的组合:

终端演示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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) : n

... skipping.
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!
然后,按 Y 继续。

系统将询问是否删除匿名用户和测试数据库,是否禁用root远程登录。

现在,我们就已经在CentOS 8服务器上安装了MySQL,并已经为root用户设置了密码。接下来,我们将测试MySQL数据库是否正常工作。

测试MySQL

使用以下命令以root(-u root)身份连接到MySQL ,并返回MySQL版本信息:

1
mysqladmin -u root -p version

我们将看到类似下面的内容输出:

终端演示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@VM-16-17-centos ~]# mysqladmin -u root -p version
Enter password:
mysqladmin Ver 8.0.26 for Linux on x86_64 (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.

Server version 8.0.26
Protocol version 10
Connection Localhost via UNIX socket
UNIX socket /var/lib/mysql/mysql.sock
Uptime: 1 hour 16 min 9 sec

Threads: 2 Questions: 17 Slow queries: 0 Opens: 133 Flush tables: 3 Open tables: 49 Queries per second avg: 0.003

这说明我们的MySQL已经安装成功。

如果想连接到MySQL,并向MySQL中添加数据,执行如下命令:

1
mysql -u root -p

与mysqladmin命令类似,此命令也包括 -u 参数,该参数可让我们指定要连接的用户,在本示例中指定要连接的用户为root。后面的 -p 参数,该参数作用是要输入为用户设置的密码。

输入了root 用户密码后,我们将进入MySQL提示符状态:

终端演示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@VM-16-17-centos ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
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

1
mysql> \q

远程不能连接问题

按上面方法进入: mysql -u root -p

1
use mysql;
终端演示
1
2
3
4
5
6
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql>
1
select host from user where user='root';
终端演示
1
2
3
4
5
6
7
8
9
mysql> select host from user where user='root';
+-----------+
| host |
+-----------+
| localhost |
+-----------+
1 row in set (0.00 sec)

mysql>

修改帐号不允许从远程登陆,只能在localhost。这个时候只要在localhost的那台电脑,登入MySQL后,更改 “mysql” 数据库里的 “user” 表里的 “host” 项,从”localhost”改称”%”

1
update user set host = '%' where user ='root';
终端演示
1
2
3
4
5
mysql> update user set host = '%' where user ='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql>

刷新配置。

1
flush privileges;
终端演示
1
2
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
DBeaver测试连接成功

资料来源:牛奇网可行的code