Ubuntu下远程访问MySQL数据库

江枫思渺然 提交于 2020-03-10 18:17:07

MySQL远程访问的命令

格式: mysql -h主机地址 -u用户名 -p用户密码

jack@jack:~$ mysql -h192.168.5.154 -usaledata -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 587
Server version: 5.5.38-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

 

示例:

yanggang@host:~$ mysql -h192.168.1.11 -uroot -p123456
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.1.11' (111)

出现错误!无法连接远程的MySQL数据库

 

MySQL远程连接数据库,有两种方式:

mysql.sock和TCP/IP,前者比后者连接访问速度更快,但仅限于同一台本机,详见

上述错误,是没有远程访问权限导致的

 

解决方法:

 

1  在目标服务器上(192.168.1.11),修改mysql的my.cnf文件: 

sudo vi /etc/mysql/my.cnf

 

2  注释掉bind-address,屏蔽其只对本地监听

#bind-address = 127.0.0.1

 

3  启动MySQL服务,使其修改的配置生效,详见

sudo restart mysql

配置完了服务器的数据访问权限,此时还是不能远程访问MySQL数据库

这是因为现在还没有对服务器上的数据库或表赋予访问权限(GRANT)

 

4  在服务器上,登录MySQL数据库

mysql -u root -p123456

 

5  对数据库top800赋予权限

grant all privileges on top800.* to root@192.168.1.22 identified by '123456';

flush privileges;

 

6  现在可以远程访问服务器上的MySQL

mysql -h192.168.1.11 -uroot -p123456

默认,只能访问information_schema和top800,其中top800是我们在步骤5赋予权限的数据库

 

 

知识拓展:


1  在服务器上删除用户对数据库的访问权限:

revoke all privileges on top800.* from root@192.168.1.22 identified by '123456';

 

2  在服务器上删除用户root:

delete from user where user='yanggang';

 

3  在修改生效:

flush privileges;

 

4  在服务器上查询用户:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!