当前位置: 移动技术网 > IT编程>数据库>Mysql > 远程连接mysql 授权方法详解

远程连接mysql 授权方法详解

2017年12月12日  | 移动技术网IT编程  | 我要评论
今在服务器上 有mysql 数据库,远程访问,不想公布root账户,所以,创建了demo账户,允许demo账户在任何地方都能访问mysql数据库中shandong库。
方案一
在安装mysql的机器上运行:
1: 创建user用户
复制代码 代码如下:

create user demo identified by “123456”

2、
复制代码 代码如下:

mysql>grant all privileges on shandong.* to 'demo'@'%'with grant option
//赋予任何主机访问数据的权限,也可以如下操作
grant all privileges on shandong.* to 'demo'@'%'identified by '123456' with grant option;

3、
复制代码 代码如下:

mysql>flush privileges
//修改生效

4、
复制代码 代码如下:

mysql>exit
//退出mysql服务器,这样就可以在其它任何的主机上以demo身份登录

引用
另外,当用客户端连接 mysql 时,发现无法连接,看来需要对用户进行重新授权。操作如下:
[root@cicro108 mysql]# bin/mysql -uroot -p -h 127.0.0.1 -a cws3
enter password:
welcome to the mysql monitor. commands end with or /g.
your mysql connection id is 1863 to server version: 4.1.20-standard
type 'help;' or '/h' for help. type '/c' to clear the buffer.
mysql> grant all privileges on *.* to root@"%" identified by "mysql" ;
query ok, 0 rows affected (0.17 sec)
发现这样更改权限以后,远程仍然不能连接,但是用下面的操作就可以了。
mysql> grant all privileges on *.* to root@"%" identified by "mysql" with grant option;
query ok, 0 rows affected (0.17 sec)
此刻, root 可以被远程连接,当然这里建立其他非 root 用户也可以远程连接。

方案二
mysql 1130错误解决方法:
通过mysql-front或mysql administrator连接mysql的时候发生的这个错误
error 1130: host ***.***.***.*** is not allowed to connect to this mysql server
说明所连接的用户帐号没有远程连接的权限,只能在本机(localhost)登录。
需更改 mysql 数据库里的 user表里的 host项
把localhost改称%

具体步骤:登陆到mysql
首先 use mysql;
按照别人提供的方式update的时候,出现错误。
mysql> update user set host='%' where user = 'root';
error 1062 (23000): duplicate entry '%-root' for key 'primary'
然后查看了下数据库的host信息如下:
mysql> select host from user where user = 'root';
+-----------------------+
| host |
+-----------------------+
| % |
| 127.0.0.1 |
| localhost.localdomain |
+-----------------------+
3 rows in set (0.00 sec)
host已经有了%这个值,所以直接运行命令:
复制代码 代码如下:

mysql>flush privileges;

再用mysql administrator连接...成功!!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网