第一,首先能ping通mysql数据库所在的主机。
第二,telnet mysql数据库的端口号 。如果不行,要在防火墙,高级设置里面,配置入站规则;开放3306端口。
第三,如果还不行,结合下面两个网址
http://blog.csdn.net/a19881029/article/details/50805562
,总结如下:
第一步:
mysql服务没问题:
- sean@sean:~$ ps -ef|grep mysqld
- mysql 1219 1 0 21:09 ? 00:00:01 /usr/sbin/mysqld
- sean 10373 9602 0 21:38 pts/7 00:00:00 grep --color=auto mysqld
并且本地的登录也能成功:
- sean@sean:~$ mysql -u root -h 127.0.0.1
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 40
- Server version: 5.5.47-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.
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- mysql>
但是使用外网地址却无法登录:
- sean@sean:~$ mysql -u root -h 192.168.137.128
- ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.137.128' (111)
于是修改了一下MySQL的配置文件:
- sean@sean:~$ sudo vi /etc/mysql/my.cnf
在bind-address= 127.0.0.1这一行前加#(注释掉这行)
- # Instead of skip-networking the default is now to listen only on
- # localhost which is more compatible and is not less secure.
- #bind-address = 127.0.0.1
然后重启mysql服务:
- sean@sean:~$ sudo service mysql restart
- mysql stop/waiting
- mysql start/running, process 11622
原先的问题解决了,现在遇到了新的问题:
再次搜索了一下,发现是授权的问题
- 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
- mysql> select host, user, password from user;
- +-----------+------------------+-------------------------------------------+
- | host | user | password |
- +-----------+------------------+-------------------------------------------+
- | localhost | root | |
- | sean | root | |
- | 127.0.0.1 | root | |
- | ::1 | root | |
- | localhost | debian-sys-maint | *0AA379AB8AFD785B32D661A07E9D5C7A24E3B186 |
- +-----------+------------------+-------------------------------------------+
- 5 rows in set (0.00 sec)
- mysql> update user set host = "%" where host = "sean" and user = "root";
- Query OK, 1 row affected (0.00 sec)
- Rows matched: 1 Changed: 1 Warnings: 0
- mysql> flush privileges;
- Query OK, 0 rows affected (0.00 sec)
- mysql> select host, user, password from user;
- +-----------+------------------+-------------------------------------------+
- | host | user | password |
- +-----------+------------------+-------------------------------------------+
- | localhost | root | |
- | % | root | |
- | 127.0.0.1 | root | |
- | ::1 | root | |
- | localhost | debian-sys-maint | *0AA379AB8AFD785B32D661A07E9D5C7A24E3B186 |
- +-----------+------------------+-------------------------------------------+
- 5 rows in set (0.00 sec)
http://www.111cn.net/database/mysql/46377.htm
最简单的办法是
MySQL远程配置
代码如下 | 复制代码 |
|
打开3306端口,为防火墙设置例外,放行3306.
但你必须有root权限了,
还可以如下方法修改:
1:在服务端MySQL文件夹下找到my.ini文件。修改bind-address=127.0.0.1 为 bind-address=0.0.0.0 (在MySQL 5的my.ini中未发现此项)
2:重新启动MySQL服务。
测试连接情况:
如果没有给远程登录用户添加所有机器都能访问的权限,将会出现“1045-Access denied for user root@IT(using password:NO)”,这表明需要添加权限;
添加命令如下:
代码如下 | 复制代码 |
1)grant all on *.* to 用户名@"%" identified by "密码"; 2)flush privileges; |
完成以上步骤,就可以远程访问MySQL数据库了。
如果上面办法不能解决我们可以开启MySQL远程访问权限 允许远程连接
1、登陆mysql数据库
代码如下 | 复制代码 |
mysql -u root -p |
查看user表
代码如下 | 复制代码 |
mysql> use mysql; Database changed mysql> select host,user,password from user; +--------------+------+-------------------------------------------+ | host | user | password | +--------------+------+-------------------------------------------+ | localhost | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E | | 192.168.1.1 | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E | +--------------+------+-------------------------------------------+ 2 rows in set (0.00 sec) |
可以看到在user表中已创建的root用户。host字段表示登录的主机,其值可以用IP,也可用主机名,
(1)有时想用本地IP登录,那么可以将以上的Host值改为自己的Ip即可。
2、实现远程连接(授权法)
将host字段的值改为%就表示在任何客户端机器上能以root用户登录到mysql服务器,建议在开发时设为%。
update user set host = ’%’ where user = ’root’;
将权限改为ALL PRIVILEGES
代码如下 | 复制代码 |
mysql> use mysql; mysql> select host,user,password from user; |
这样机器就可以以用户名root密码root远程访问该机器上的MySql.
3、实现远程连接(改表法)
代码如下 | 复制代码 |
use mysql; update user set host = '%' where user = 'root'; |
这样在远端就可以通过root用户访问Mysql
总结:
看过上面的介绍后,想必很容易理解了吧,首先数据库服务要启动,其次连接数据库的主机要开放连接数据库入站规则的端口3306,再其次就是在数据库中给远程连接的主机赋予远程连接的权限。