【Linux】之远程连接 MySQL 服务器

古城微笑少年丶 2023-03-01 08:41 236阅读 0赞

默认情况下,mysql 只允许本地登录,查询MySQL数据库是否允许远程ip访问,命令如下:

  1. mysql> use mysql;
  2. Reading table information for completion of table and column names
  3. You can turn off this feature to get a quicker startup with -A
  4. Database changed
  5. mysql> select host,user from user;
  6. +-----------+---------------+
  7. | host | user |
  8. +-----------+---------------+
  9. | localhost | mysql.session |
  10. | localhost | mysql.sys |
  11. | localhost | root |
  12. +-----------+---------------+

如果查询结果为127.0.0.1或者localhost或者当前服务器域名,则表示不允许远程连接

下面以 CentOS 7 为例,演示如何连接远程服务器的 MySQL,比如阿里云服务器

一、修改/etc/mysql/my.conf**

  1. vim /etc/my.cnf

找到bind-address = 127.0.0.1这一行
改为bind-address = 0.0.0.0即可

二、为需要远程登录的用户赋予权限

1、新建用户远程连接mysql数据库

  1. grant all on *.* to admin@'%' identified by '123456' with grant option;
  2. flush privileges;

允许任何 ip 地址(%表示允许任何ip地址)的电脑用 admin 用户和密码(123456)来访问这个mysql server。

注意:admin 账户不一定要存在,这里只是授权给 admin 这个用户而已。

2、支持root用户允许远程连接mysql数据库

  1. grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
  2. flush privileges;

三、再次查看系统用户

  1. mysql> use mysql;
  2. Reading table information for completion of table and column names
  3. You can turn off this feature to get a quicker startup with -A
  4. Database changed
  5. mysql> select host,user from user;
  6. +-----------+---------------+
  7. | host | user |
  8. +-----------+---------------+
  9. | % | root |
  10. | localhost | mysql.session |
  11. | localhost | mysql.sys |
  12. | localhost | root |
  13. +-----------+---------------+

从上面可以看出已经配置好了允许远程连接 root 用户。

注意:如果想要远程连接阿里云服务器的 MySQL,需要开放 MySQL 对应的 3306 端口。

发表评论

表情:
评论列表 (有 0 条评论,236人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Linux下Telnet远程连接服务器

    > Telnet协议是TCP/IP协议族中的一员,是Internet远程登陆服务的标准协议和主要方式。它为用户提供了在本地计算机上完成远程主机工作的能力。在终端使用者的电脑上使