红联Linux门户
Linux帮助

mysql创建新用户并设权限命令

发布时间:2008-06-19 00:05:38来源:红联作者:Intottle
当使用MySQL的时候,发现使用这样的命令mysql -h 127.0.0.1 -u mysql -p 不能访问数据库

root#mysql -h 127.0.0.1 -u mysql -p
Enter password:******
ERROR 1045: Access denied for user: 'mysql@127.0.01' (Using password: YES)

原因:
在127.0.0.1上的用户mysql没有连接localhost上MySQL的权限,可以通过如下的方式确认:

root#mysql -h localhost-u mysql -p
Enter password: ******

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4 to server version: 4.0.20a-debug

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> use mysql; (此DB存放MySQL的各种配置信息)
Database changed
mysql> select host,user from user; (查看用户的权限情况)
+-------------+-------+
| host | user |
+-------------+-------+
| localhost | |
| localhost | root |
| localhost | |
| localhost | mysql |
+-------------+-------+
6 rows in set (0.02 sec)

由此可以看出,只能以localhost的主机方式访问。

解决方法:

mysql> Grant all privileges on *.* to 'root'@'%' identified by ******* with grant option;
Query OK, 0 rows affected (0.02 sec) (%表示是所有的外部机器,如果指定某一台机,就将%改为相应的机器名)
mysql> Grant all privileges on *.* to 'mysql'@'%' identified by ******* with grant option;
Query OK, 0 rows affected (0.02 sec) (%表示是所有的外部机器,如果指定某一台机,就将%改为相应的机器名)

mysql> flush privileges; (运行为句才生效,或者重启MySQL)
Query OK, 0 rows affected (0.03 sec)

mysql> select host,user from user; (再次查看用户的权限情况)
+-------------+-------+
| host | user |
+-------------+-------+

| % | mysql |

| % | root |
| localhost | |
| localhost | root |
| localhost | |
| localhost | mysql |
+-------------+-------+

mysql>exit

现在再试试:
root#mysql -h mysql -u root -p
Enter password:******
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9 to server version: 4.0.20a-debug

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

就成功连接上了。

注意:

以上的设置不紧是对本机的用户使用权限的更改,在所有外部的机器上都可以使用指定的用户登陆连接。当使用mysql-front在windows下管理数据库时,可能由于版本的问题有些程序不支持使用密码,出现以下提示:

1251 - Client does not support authentication protocol requested by server; consider upgrading MySQL client

可以使用mymanager来管理mysql。
文章评论

共有 0 条评论