1. 登录xshell连接linux服务器,登录MySQL客户端
mysql -u root -p
2. 配置访问用户和权限
按以下设置远程访问的用户名(lanlan)和密码(123456)以及使用范围(%,可以是某IP地址),如下设置:
mysql> grant all privileges on *.* to 'lanlan'@'%' identified by '123456' with grant option;Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
查看是否创建成功:
mysql> select distinct concat('user: ''',user,'''@''',host,''';') as query from mysql.user;
+---------------------------------------+
| query |
+---------------------------------------+
| user: 'lanlan'@'%'; |
| user: 'root'@'127.0.0.1'; |
| user: 'root'@'::1'; |
| user: 'root'@'lan'; |
| user: 'debian-sys-maint'@'localhost'; |
| user: 'phpmyadmin'@'localhost'; |
| user: 'root'@'localhost'; |
+---------------------------------------+
提示成功后退出MySQL客户端 \q
3. 编辑文件mysqld.conf
sudo vim /etc/mysql/my.cnf
找到 “bind-address = 127.0.0.1” ,在前面#,即 #bind-address = 127.0.0.1 然后保存并重启MySQL:
root@lan:/home/brisk# /etc/init.d/mysql stop
mysql stop/waiting
root@lan:/home/brisk# /etc/init.d/mysql start
mysql start/running, process 3735
sudo ufw allow 3306
sudo ufw enable (开启防火墙需要输入‘y’确认)
【这是个坑,后面连ssh都成问题,要重新配置防火墙】
查看防火墙状态
sudo ufw status
To Action From
-- ------ ----
3306 ALLOW Anywhere
3306 (v6) ALLOW Anywhere (v6)
好了,配置好之后可以远程连接访问数据库了,我用navigat