第一,首先能ping通mysql数据库所在的主机。php
第二,telnet mysql数据库的端口号 。若是不行,要在防火墙,高级设置里面,配置入站规则;开放3306端口。html
第三,若是还不行,结合下面两个网址mysql
http://blog.csdn.net/a19881029/article/details/50805562sql
,总结以下:数据库
第一步:ubuntu
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
而且本地的登陆也能成功:less
- 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>
可是使用外网地址却没法登陆:ide
- 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
有朋友可能会碰到使用Navicat for mysql 远程链接 mySql数据库会提示1006一、1045错误或 2003-Can’t connect to MySQL on ’192.168.1.2’(10061),这个缘由是由于MySQL不许许远程链接。
最简单的办法是
MySQL远程配置
代码以下 |
复制代码 |
GRANT ALL PRIVILEGES ON *.* TO root@'%' IDENTIFIED BY 'your paaaword'; %表示多有机器。 |
打开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远程访问权限 容许远程链接
一、登录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便可。
二、实现远程链接(受权法)
将host字段的值改成%就表示在任何客户端机器上能以root用户登陆到mysql服务器,建议在开发时设为%。
update user set host = ’%’ where user = ’root’;
将权限改成ALL PRIVILEGES
代码以下 |
复制代码 |
mysql> use mysql; Database changed mysql> grant all privileges on *.* to root@'%' identified by "root"; Query OK, 0 rows affected (0.00 sec) mysql> select host,user,password from user; +--------------+------+-------------------------------------------+ | host | user | password | +--------------+------+-------------------------------------------+ | localhost | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E | | 192.168.1.1 | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E | | % | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E | +--------------+------+-------------------------------------------+ 3 rows in set (0.00 sec) |
这样机器就能够以用户名root密码root远程访问该机器上的MySql.
三、实现远程链接(改表法)
代码以下 |
复制代码 |
use mysql; update user set host = '%' where user = 'root'; |
这样在远端就能够经过root用户访问Mysql
总结:
看过上面的介绍后,想必很容易理解了吧,首先数据库服务要启动,其次链接数据库的主机要开放链接数据库入站规则的端口3306,再其次就是在数据库中给远程链接的主机赋予远程链接的权限。