一、查看Linux版本
shell
cat /etc/os-release
NAME="Alibaba Cloud Linux"
VERSION="3 (Soaring Falcon)"
ID="alinux"
二、查看是否安装mysql
shell
sudo systemctl status mysqld
Unit mysqld.service could not be found.(表示未安装)
三、依次执行下面命令
shell
# 安装一个兼容库(Alibaba Cloud Linux 3需要)
sudo yum install -y compat-openssl10
# 添加MySQL官方软件源
sudo rpm -Uvh https://repo.mysql.com/mysql84-community-release-el8-1.noarch.rpm
# 安装MySQL服务
sudo dnf install -y mysql-server
# 启动MySQL服务并设为开机自启
sudo systemctl start mysqld
sudo systemctl enable mysqld
安装完成后检查一下服务状态
shell
sudo systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.servi>
Active: active (running) since Tue 2026-05-26 21:42:>
Docs: man:mysqld(8)
四、安装后配置:
1、获取临时密码:首次启动会自动生成一个临时密码,用它才能登录。
shell
grep 'temporary password' /var/log/mysqld.log
2026-05-26T13:42:13.633579Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: wdjkgdqj-2yX
最后的字符串就是临时密码 。
2、进行安全配置:运行安全脚本可以设置新密码、移除匿名用户等,让数据库更安全。
shell
mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
输入刚刚的临时密码,后面会有如下步骤
- 设置新的
root密码(要求至少8位,包含大写、小写、数字和特殊字符) - 是否移除匿名用户?建议输入
y。 - 是否禁止root用户远程登录?建议输入
y。 - 是否移除测试数据库?建议输入
y。 - 是否重新加载权限表?建议输入
y。
3、最后需要在阿里云安全组里面开放端口,否则无法远程访问
附:
1、 修改默认端口3306
shell
vim /etc/my.cnf
在 [mysqld] 部分下添加
shell
[client]
port = 3307
[mysqld]
port = 3307
然后重启服务
shell
sudo systemctl restart mysqld
没有任何输出表示成功
如果报错请到vim /var/log/mysqld.log查看原因,比如下面就是报错了。
shell
sudo systemctl restart mysqld;
Job for mysqld.service failed because the control process exited with error code.
2、让mysql可以远程访问
如果Navicat连接时报错:not allowed to connect to this mysql
则需要配置远程用户,先在shell里登陆mysql,输入密码后登陆
shell
mysql -u root -p
Enter password:
mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 8.4.9 |
+-----------+
mysql> SELECT user, host FROM mysql.user WHERE user = 'root';
+------+-----------+
| user | host |
+------+-----------+
| root | localhost |
+------+-----------+
# 上面输出说明只有localhost权限,没有 %(任意IP)的用户
创建用户
shell
-- 创建允许从任意IP连接的用户
mysql>CREATE USER 'root'@'%' IDENTIFIED BY 'Qf2026@#';
Query OK, 0 rows affected (0.02 sec)
-- 授予所有权限
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)
-- 刷新权限
mysql>FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
现在就可以使用navicat远程连接了