在阿里云上搭建好了Mysql,希望可以用远程访问。这个时候就需要设置一下阿里云服务器的一些配置。
1.先登录阿里云的Mysql数据库
先远程到阿里云的服务器
执行脚本 mysql -u root -h localhost -p 【其中root表示数据库登录名】
再输入数据库的密码
root@iZm5ehcixm5361wjbi0od4Z:~# mysql -u root -h localhost -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.24-0ubuntu0.16.04.1 (Ubuntu)
...(过多省略)
affiliates. Other names may be trademarks of their respective
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
2.打开mysql数据库
在 mysql命令模式下执行脚本 use mysql
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
3.设置可以访问的用户
- 更新user=‘root’ and host=‘localhost’ 的host数据
1).host设置为 % 或者 设置为 指定的IP 表示任何IP进行访问该数据库 (笔者就是用这种)
eg: update user set host=’%’ where user=‘root’ and host=‘localhost’;
2).host设置为指定的ip 表示只有该IP才能访问该数据库
eg: update user set host=‘180.168.134.210’ where user=‘root’ and host=‘localhost’; - 创建一个新用户
执行命令 => grant 权限列表 on 数据库 to ‘用户名’@‘访问主机’ identified by ‘密码’;
eg: grant all on . to ‘newusername’@’%’ identified by ‘Navicat_123’;
mysql> update user set host='%' where user='root' and host='localhost';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
4.刷新权限表,让上面的改动生效
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
5.确认阿里云的端口是否开放
mysql的默认端口是3306,如果阿里云上设置未设置对外,可以[ 点击这里去看看阿里云的安全规则配置文档 ]