mysql数据库开启远程访问

发布于 2023-02-22  690 次阅读


默认情况下,MySQL只允许本地登录,即只能在安装MySQL环境所在的主机下访问。
但是在日常开发和使用中,我们经常需要访问远端服务器的数据库,此时就需要开启服务器端MySQL的远程连接权限。
环境:Linux
软件:mysql
1、生成环境,连接mysql
mysql -u root -p密码

2、查看MySQL当前访问远程访问权限
use mysql;
select User,authentication_string,Host from user;

输出如下

+------+-----------------------+--------------+
| User | authentication_string | Host         |
+------+-----------------------+--------------+
| root |                       | localhost    |
| root |                       | chino-centos |
| root |                       | 127.0.0.1    |
| root |                       | ::1          |
+------+-----------------------+--------------+

当前root用户,host为只允许本地登录

3、开启远程访问
update user set host='%' where user='root';
然后再查询
select User,authentication_string,Host from user;

+------+-----------------------+--------------+
| User | authentication_string | Host         |
+------+-----------------------+--------------+
| root |                       | %            |
| root |                       | chino-centos |
| root |                       | 127.0.0.1    |
| root |                       | ::1          |
+------+-----------------------+--------------+

之后就可以进行远程访问了,但在重启mysql之前连接并不会成功。

注:版本5.6以上版本的authentication_string可能会显示秘钥内容,跟着教程走就行了。该教程通用于mysql 5.5-8.0


欢迎来到我的小窝 戳下面的箭头可以切换背景哦~