云服务器安装 MySQL
本文主要介绍在云服务器中安装 MySQL 的步骤,并配置使任何 IP 都能访问。
云服务器安装 MySQL
1. 前言
最近自己的项目需要用到云数据库,考虑到以后可能还会用到服务器做其他事情,于是先租了最便宜的阿里云服务器。
本文主要介绍在云服务器中安装 MySQL 的步骤,并配置使任何 IP 都能访问。
2. 远程连接
FinalShell 连接
3. 具体步骤
仅介绍部分关键步骤,其他步骤可参考 References。
3.1 查询并卸载 mariadb
1
2
3
4
5
[root@iZ2zehzifcudq4xw9etzyiZ ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
[root@iZ2zehzifcudq4xw9etzyiZ ~]# rpm -e --nodeps mariadb-libs
[root@iZ2zehzifcudq4xw9etzyiZ ~]# rpm -qa|grep mariadb
[root@iZ2zehzifcudq4xw9etzyiZ ~]#
3.2 安装 MySQL
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
[root@iZ2zehzifcudq4xw9etzyiZ ~]# tar -xvf mysql-8.0.28-1.el7.x86_64.rpm-bundle.tar
mysql-community-client-8.0.28-1.el7.x86_64.rpm
mysql-community-client-plugins-8.0.28-1.el7.x86_64.rpm
mysql-community-common-8.0.28-1.el7.x86_64.rpm
mysql-community-devel-8.0.28-1.el7.x86_64.rpm
mysql-community-embedded-compat-8.0.28-1.el7.x86_64.rpm
mysql-community-icu-data-files-8.0.28-1.el7.x86_64.rpm
mysql-community-libs-8.0.28-1.el7.x86_64.rpm
mysql-community-libs-compat-8.0.28-1.el7.x86_64.rpm
mysql-community-server-8.0.28-1.el7.x86_64.rpm
mysql-community-test-8.0.28-1.el7.x86_64.rpm
[root@iZ2zehzifcudq4xw9etzyiZ ~]# ll
总用量 1646504
-rw-r--r-- 1 root root 843008000 2月 24 23:01 mysql-8.0.28-1.el7.x86_64.rpm-bundle.tar
-rw-r--r-- 1 7155 31415 55199948 12月 18 2021 mysql-community-client-8.0.28-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 5933684 12月 18 2021 mysql-community-client-plugins-8.0.28-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 645388 12月 18 2021 mysql-community-common-8.0.28-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 7763684 12月 18 2021 mysql-community-devel-8.0.28-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 23637584 12月 18 2021 mysql-community-embedded-compat-8.0.28-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 2215928 12月 18 2021 mysql-community-icu-data-files-8.0.28-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 4935572 12月 18 2021 mysql-community-libs-8.0.28-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 1265072 12月 18 2021 mysql-community-libs-compat-8.0.28-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 473116268 12月 18 2021 mysql-community-server-8.0.28-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 268279684 12月 18 2021 mysql-community-test-8.0.28-1.el7.x86_64.rpm
[root@iZ2zehzifcudq4xw9etzyiZ ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
[root@iZ2zehzifcudq4xw9etzyiZ ~]# rpm -e --nodeps mariadb-libs
[root@iZ2zehzifcudq4xw9etzyiZ ~]# rpm -qa|grep mariadb
[root@iZ2zehzifcudq4xw9etzyiZ ~]# rpm -ivh mysql-community-common-8.0.28-1.el7.x86_64.rpm
警告:mysql-community-common-8.0.28-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-common-8.0.28-1.e################################# [100%]
[root@iZ2zehzifcudq4xw9etzyiZ ~]# rpm -ivh mysql-community-client-plugins-8.0.28-1.el7.x86_64.rpm
警告:mysql-community-client-plugins-8.0.28-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-client-plugins-8.################################# [100%]
[root@iZ2zehzifcudq4xw9etzyiZ ~]# rpm -ivh mysql-community-libs-8.0.28-1.el7.x86_64.rpm
警告:mysql-community-libs-8.0.28-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-libs-8.0.28-1.el7################################# [100%]
[root@iZ2zehzifcudq4xw9etzyiZ ~]# rpm -ivh mysql-community-client-8.0.28-1.el7.x86_64.rpm
警告:mysql-community-client-8.0.28-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-client-8.0.28-1.e################################# [100%]
[root@iZ2zehzifcudq4xw9etzyiZ ~]# rpm -ivh mysql-community-icu-data-files-8.0.28-1.el7.x86_64.rpm
警告:mysql-community-icu-data-files-8.0.28-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-icu-data-files-8.################################# [100%]
[root@iZ2zehzifcudq4xw9etzyiZ ~]# rpm -ivh mysql-community-server-8.0.28-1.el7.x86_64.rpm
警告:mysql-community-server-8.0.28-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
错误:依赖检测失败:
libaio.so.1()(64bit) 被 mysql-community-server-8.0.28-1.el7.x86_64 需要
libaio.so.1(LIBAIO_0.1)(64bit) 被 mysql-community-server-8.0.28-1.el7.x86_64 需要
libaio.so.1(LIBAIO_0.4)(64bit) 被 mysql-community-server-8.0.28-1.el7.x86_64 需要
[root@iZ2zehzifcudq4xw9etzyiZ ~]# yum install libaio
已加载插件:fastestmirror
Determining fastest mirrors
base | 3.6 kB 00:00:00
epel | 4.3 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/7): epel/x86_64/updateinfo | 1.0 MB 00:00:00
(2/7): base/7/x86_64/group_gz | 153 kB 00:00:00
(3/7): base/7/x86_64/primary_db | 6.1 MB 00:00:00
(4/7): epel/x86_64/group | 399 kB 00:00:00
(5/7): extras/7/x86_64/primary_db | 253 kB 00:00:00
(6/7): epel/x86_64/primary_db | 8.7 MB 00:00:00
(7/7): updates/7/x86_64/primary_db | 27 MB 00:00:00
正在解决依赖关系
--> 正在检查事务
---> 软件包 libaio.x86_64.0.0.3.109-13.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
===================================================================================================================================================================
Package 架构 版本 源 大小
===================================================================================================================================================================
正在安装:
libaio x86_64 0.3.109-13.el7 base 24 k
事务概要
===================================================================================================================================================================
安装 1 软件包
总下载量:24 k
安装大小:38 k
Is this ok [y/d/N]: y
Downloading packages:
libaio-0.3.109-13.el7.x86_64.rpm | 24 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
** 发现 2 个已存在的 RPM 数据库问题, 'yum check' 输出如下:
2:postfix-2.10.1-9.el7.x86_64 有缺少的需求 libmysqlclient.so.18()(64bit)
2:postfix-2.10.1-9.el7.x86_64 有缺少的需求 libmysqlclient.so.18(libmysqlclient_18)(64bit)
正在安装 : libaio-0.3.109-13.el7.x86_64 1/1
验证中 : libaio-0.3.109-13.el7.x86_64 1/1
已安装:
libaio.x86_64 0:0.3.109-13.el7
完毕!
[root@iZ2zehzifcudq4xw9etzyiZ ~]# rpm -ivh mysql-community-server-8.0.28-1.el7.x86_64.rpm
警告:mysql-community-server-8.0.28-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-server-8.0.28-1.e################################# [100%]
[root@iZ2zehzifcudq4xw9etzyiZ ~]#
3.3 一些设置
1
2
3
4
[root@iZ2zehzifcudq4xw9etzyiZ ~]# mysqld --initialize --console
[root@iZ2zehzifcudq4xw9etzyiZ ~]# chown -R mysql:mysql /var/lib/mysql
[root@iZ2zehzifcudq4xw9etzyiZ ~]# systemctl start mysqld
[root@iZ2zehzifcudq4xw9etzyiZ ~]# cat /var/log/mysqld.log | grep localhost
3.4 改密码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2025-02-24T15:22:22.548855Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: s%kW&Ziok5en
[root@iZ2zehzifcudq4xw9etzyiZ ~]#
[root@iZ2zehzifcudq4xw9etzyiZ ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.28
Copyright (c) 2000, 2022, 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> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password'
-> ;
Query OK, 0 rows affected (0.01 sec)
mysql> ^C
3.5 通过 SQL 文件导入数据库
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
[root@iZ2zehzifcudq4xw9etzyiZ ~]# mysql -u root -p db_name < db_name.sql
Enter password:
[root@iZ2zehzifcudq4xw9etzyiZ ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.28 MySQL Community Server - GPL
Copyright (c) 2000, 2022, 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> show database
-> ;
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 'database' at line 1
mysql> use db_name
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> show table;
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 '' at line 1
mysql> show tables;
+---------------------------------+
| Tables_in_db_name |
+---------------------------------+
| tabel_1 |
| tabel_2 |
| tabel_3 |
+---------------------------------+
8 rows in set (0.01 sec)
3.6 MySQL 权限设置
1
2
3
4
5
6
7
8
-- 创建用户
CREATE USER 'root'@'%' IDENTIFIED BY '[密码]';
-- 授予权限
GRANT ALL PRIVILEGES ON [数据库名].* TO 'root'@'%';
-- 刷新权限
FLUSH PRIVILEGES;
3.7 3306 端口的设置,可以监听 IPV4
确保 MySQL 配置文件(通常位于 /etc/my.cnf)中的 bind-address 参数设置为 0.0.0.0,以允许远程连接:
1
bind-address = 0.0.0.0
重启 MySQL 服务使生效
1
sudo systemctl restart mysqld
检查
1
2
[root@localhost Documents]# sudo netstat -tulnp | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 108253/mysqld
3.8 配置阿里云安全组
阿里云服务器默认会阻止外部访问 MySQL 的端口(默认是 3306),需要在安全组中放行该端口。
- 登录阿里云控制台。
- 进入 ECS 实例管理。
- 找到你的服务器实例,点击 安全组。
- 在安全组规则中,添加一条入方向规则:
- 协议类型:
MySQL (3306) - 授权对象:
0.0.0.0/0(允许所有 IP 访问)或指定你的客户端 IP 地址。
- 协议类型:
- 保存规则。
3.9 远程连接 MySQL 测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
C:\>mysql -u root -p -h [服务器 IP] --ssl-mode=DISABLED
Enter password: ******
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.28 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.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 报错
C:\Users>mysql -u root -p -h [服务器 IP] --ssl-mode=DISABLED
Enter password: ******
ERROR 2061 (HY000): Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.
# 解决方案
# 登录到 MySQL 服务器:
mysql -u root -p
# 修改用户认证插件:
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '你的密码';
FLUSH PRIVILEGES;
# 退出 MySQL:
EXIT;
# 重新尝试连接:
mysql -u root -p -h [服务器 IP] --ssl-mode=DISABLED
4. References
This post is licensed under CC BY 4.0 by the author.