MySQL设置远程访问

linux下不同版本在设置msql远程访问时略有不同。

ubuntu

1. 确认一下3306是否对外开放,MySQL默认状态下是不开放对外访问功能的。查看的办法如下:

~# netstat -an | grep 3306 
 出现:  tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN

从上面可以看出,mysql的3306端口只是监听本地的连接,这样就阻碍了外部IP对该数据库的访问

2. 修改的办法其实很简单,进入到mysql的配置文件所在目录(/etc/mysql/my.cnf)下,找到文件中的如下内容:

# 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
3. 将bind-address注释掉 (所有机器都可以访问,不限制IP) ,或者改成你想要使用的客户端主机IP。

4. 重启mysql即可在远程登陆mysql服务器了

5. 再次确认一下3306是否对外开放,MySQL默认状态下是不开放对外访问功能的。查看的办法如下:

~# netstat -an | grep 3306
出现:    tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN  

6. 给root赋予权限这样才能用root远程访问

 mysql -u root -p  #再输入密码即可重新登录mysql。
执行如下命令:

grant all on sonardb.* to sonar@'%' identified by '123456';
grant all on sonardb.* to sonar@localhost identified by '123456';
sonardb替换为你想访问的数据库名,sonar是你的想使用的用户名,123456替换为你的密码,这样就开启了远程访问功能.

 

 

 在Ubuntu下MySQL缺省是只允许本地访问的,如果你要其他机器也能远程够访问这台Mysql数据库的话,需要设置一些东西,下面我们一步步地来:

一、配置文件的修改
    1.#sudo vi /etc/mysql/my.conf

    找到 bind-address = 127.0.0.1
    注释掉这句话 
 
二、Mysql数据库的修改
1) [root@etc etc]# mysql -u  root  -p
Enter password: 
2)mysql> use mysql;
3)mysql> select host,user,password from user;
4)grant all privileges on *.* to  root@192.168.15.101  identified by 'password'
 注意: (1192.168.15.101是欲连接到此Mysql数据库的客户端的IP地址,而不是Mysql数据库所在数据库服务器的IP地址,切记
              (2)password就是Mysql数据库root用户的password,根据实际情况需要修改

三、设置防火墙
开启3306端口
#sudo ufw allow mysql


centos

 

posted @ 2014-05-18 21:19  milkty  阅读(715)  评论(0)    收藏  举报