Linux学习笔记之——ubuntu中mysql允许远程连接


Linux学习笔记之——ubuntu中mysql允许远程连接

摘要:一般mysql默认安装出于安全考虑、一般只会让主机连接到mysql、而其他的机器通过远程的方式是连接不上mysql的。这样想在别的机器上远程操作主机的mysql就会denied、当然备份也会被拒绝。记录一下如何解决mysql支持远程。

一:简介

环境依然是前面的环境、可以在其他机器上测试一下是否能远程连接本主机的mysql。我主机的IP是192.168.26.200、mysql用户是root、密码是password、键入如下命令、并输入密码:

mysql–h192.168.26.200 –uroot –p

会提示:

ERROR 2003(HY000): Can't connect to MySQL server on '192.168.26.200' (111)

问题就是我们并没有开启主机、也就是192.168.26.200上的mysql的允许别人远程的配置。

二:解决过程

2.1 查看主机上mysql允许连接的机器IP

a) 登录mysql、输入命令:

                mysql  –uroot  –p

b) 输入密码:

                password

c) mysql的相关权限都在mysql这个自带的database内、查看user表的指定字段

                select user,password , host from user where user = 'root';
                #会显示如下
                +------+-------------------------------------------+-----------+
                | user |password                                  | host      |
                +------+-------------------------------------------+-----------+
                | root |*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 | localhost |
                +------+-------------------------------------------+-----------+
                说明root用户登录下的mysql只允许localhost连接。

2.2 实现所有IP可以连接root用户的mysql。

a) 修改root用户下的mysql、即可以指定某些IP能够连接、也可以指定所有的IP可以连接、这里指定的是全部

b) 在上面的查看权限界面执行如下语句:

                grant allprivileges on *.* to 'root'@'%' identified by 'password';
                flushprivileges;

如果我们想指定IP连接、那么可以将上面的%换成我们指定的IP、比如指定192.168.30.253可以连接

                grant allprivileges on *.* to 'root'@'192.168.30.253' identified by 'password';
                flushprivileges;

修改mysql的配置文件 my.cnf中

                vim /etc/mysql/my.cnf
                bind-address = 127.0.0.1
                将 bind-address =127.0.0.1 这一行注释掉, 即修改为:
                 #bind-address = 127.0.0.1

2.3 测试

使用其他机器远程登录试试:

                mysql -h192.168.26.200 -uroot –ppassword
                #显示如下信息
                Welcome to the MySQL monitor.  Commands end with ; or \g.
                Your MySQL connection id is 413
                Server version: 5.5.35-0ubuntu0.12.04.2(Ubuntu)
 
                Copyright (c) 2000, 2011, Oracle and/or itsaffiliates. All rights reserved.
 
                Oracle is a registered trademark of OracleCorporation and/or its
                affiliates. Other names may be trademarksof their respective
                owners.
 
                Type 'help;' or '\h' for help. Type '\c' toclear the current input statement.
 
                mysql>
 
                则登录成功

三: 补充

3.1 在window下修改mysql编码:

修改mysql安装目录下的配置文件my.ini。将其中的latin1修改成utf8

3.2window下命令窗口启动、停止mysql服务

停止:

                       net stop mysql

启动:

                       net start mysql

相关内容