mysql远程连接错误1130的解决方法

转载:https://my.oschina.net/maliang0130/blog/207823


mysql中的user表中User = mysql.sys 对应的Host = localhost,这样才可以通过mysql -u root -p 登陆


解决远程连接mysql错误1130代码的方法 

今天在用远程连接Mysql服务器的数据库,不管怎么弄都是连接不到,错误代码是1130,ERROR 1130: Host 192.168.2.159 is not allowed to connect to this MySQL server  
猜想是无法给远程连接的用户权限问题。结果这样子操作mysql库,即可解决。在本机登入mysql后,更改 “mysql” 数据库里的 “user” 表里的 “host” 项,从”localhost”改称'%'。。 
mysql -u root -p 
mysql>use mysql; 
mysql>select 'host' from user where user='root'; 
mysql>update user set host = '%' where user ='root'; 
mysql>flush privileges; 
mysql>select 'host'   from user where user='root'; 
第一句是以权限用户root登录 
第二句:选择mysql库 
第三句:查看mysql库中的user表的host值(即可进行连接访问的主机/IP名称) 
第四句:修改host值(以通配符%的内容增加主机/IP地址),当然也可以直接增加IP地址 
第五句:刷新MySQL的系统权限相关表 
第六句:再重新查看user表时,有修改。。 

重起mysql服务即可完成。

==============================================================

    本机的mysql数据库中有两条user='root'的记录,将其中一条host='localhost'的host修改为'%'后,虽然可以通过远程访问数据库了,但是使用localhost或者127.0.0.1又无法访问数据库了。

    经过一番折腾,发现可以通过创建用户的方法来解决这个问题。不需要修改user表中的任何数据,在本地用root登陆mysql后,执行下面的语句,创建用户名为'root',密码为'123456'的用户。执行完以后,查看user表,发现新增了一条host='%',user='root'的记录,并且各项权限与其它'root'一样,再次使用192.168.1.13进行访问,发现可以正常访问了。

grant allon *.*to rootidentified by '123456'  with grant option;

如果访问还有问题,可以执行一下flush privileges;


================================================================


授权法。例如,你想myuser使用mypassword从任何主机连接到mysql服务器的话。
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
如果你想允许用户myuser从ip为192.168.1.3的主机连接到mysql服务器,并使用mypassword作为密码
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'192.168.0.1' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;


给用户授权 

我用的用户是root 密码123456 

首选语法为: 
Sql代码 
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION; 

示例: 
Sql代码 
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION; 

执行完后,再 
mysql:flush privileges; 
刷新一下权限就可以了,不用重启 

====================================================================


远程连接MySQL没有权限的原因有两种,一个是因为MySQL的限制,一个是防火墙的限制。

1,解决防火墙限制:

在MySQL服务主机上将防火墙关闭或者在防火墙高级设置里面加入出入站规则,加上MySQL的端口,允许通过MySQL的端口进行访问主机。

2,解决MySQL的限制,在MySQL服务主机上执行下列sql

 -- 更改root用户的登录主机为任意主机
UPDATE USER
SET Host = '%'
WHERE User = 'root';
-- 更改权限(root为账户名,%为主机名(任意主机),525099302为密码)
GRANT ALL PRIVILEGES ON *.* TO root@'%' IDENTIFIED BY "525099302";
-- 刷新权限,mysql直接生效
flush privileges;

将上述两个限制突破,就可以通畅无阻地远程访问mysql主机了。



=======================================================================


  1. Open up the file under 'etc/mysql/my.cnf'
  2. Check for:

    • port (by default this is 'port = 3306')
    • bind-address (by default this is 'bind-address = 127.0.0.1'; if you want to open to all then just comment out this line. For my example, I'll say the actual server is on 10.1.1.7)
  3. Now access the MySQL Database on your actual server (say your remote address is 123.123.123.123 at port 3306 as user 'root' and I want to change permissions on database 'dataentry'. Remember to change the IP Address, Port, and database name to your settings)

    mysql -u root -p
    Enter password: <enter password>
    mysql>GRANT ALL ON *.* to root@'123.123.123.123' IDENTIFIED BY 'put-your-password';
    mysql>FLUSH PRIVILEGES;
    mysql>exit
  4. sudo service mysql restart

  5. You should now be able to remote connect to your database. For example, I'm using MySQL Workbench and putting in 'Hostname:10.1.1.7', 'Port:3306', 'Username:root'


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值