1.安装Mysql
#命令1
sudo apt-get update
#命令2
sudo apt-get install mysql-server
2.配置Mysql
2.1初始化配置
sudo mysql_secure_installation
配置项如下
#1
VALIDATE PASSWORD PLUGIN can be used to test passwords...
Press y|Y for Yes, any other key for No: N (我的选项)
#2
Please set the password for root here...
New password: (输入密码)
Re-enter new password: (重复输入)
#3
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...
Remove anonymous users? (Press y|Y for Yes, any other key for No) : N (我的选项)
#4
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 (我的选项)
#5
By default, MySQL comes with a database named 'test' that
anyone can access...
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N (我的选项)
#6
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 (我的选项)
2.2 检查Mysql状态
systemctl status mysql.service
结果如下解释成功安装运行
3.远程访问
3.1 修改Mysql用户表
mysql -u root -p
# 切换database
use mysql;
# 查看用户表
mysql> select host,user from user;
+-----------+------------------+
| host | user |
+-----------+------------------+
| localhost | root |
+-----------+------------------+
# 更新用户表
# localhost就是本地访问,配置成`%`就是所有主机都可连接
mysql> update user set `host` = '%' where `user` = 'root' LIMIT 1;
# 分配所有权限
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root密码' WITH GRANT OPTION;
# 强制刷新权限
mysql> flush privileges;
# 查看修改后的用户表
mysql> select host,user from user;
+-----------+------------------+
| Host | User |
+-----------+------------------+
| % | root |
+-----------+------------------+
3.2修改mysqld.cnf
使用命令netstat -an|grep 3306 查看端口监听状态如下所示,绑定了127.0.0.1,只允许本地访问,需要修改配置文件:
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
进入/etc/mysql/mysql.conf.d/mysqld.cnf
注释文件中的# bind-address = 127.0.0.1
重启mysql服务service mysql restart 使用命令netstat -an|grep 3306再次查看状态
tcp6 0 0 :::3306 :::* LISTEN
4.配置阿里云安全组规则 登录阿里云进入控制台,依次访问网络和安全->安全组->配置规则,在入方向上开放MySQL监听端口3306即可,如下所示:
到此,阿里云服务器的MYSQL数据库远程访问开启成功,使用客户端测试连接成功🌹🌹🌹