MySQL:除 root 用户外,用户“test"@“localhost"的访问被拒绝(使用密码:YES)

2021-11-20 00:00:00 mysql mysql-error-1045

我遇到了 mysql 非 root/admin 用户的问题,我正在按照以下步骤创建用户及其权限,如果我做错了,请纠正我,

I am facing problem with mysql non root/admin user, I am following the below steps for creating user and its privileges, correct me if i am doing wrong,

我正在RHEL 5.7 64bit上安装mysql,下面提到了包,一旦我完成rpm install,我们就是

i am installing mysql on RHEL 5.7 64bit, packages are mentioned below, once i done the rpm install we are

  1. 使用mysql_install_db创建mysql数据库,然后
  2. 启动mysql服务然后
  3. 使用 mysql_upgrade 也是我们对服务器所做的.
  1. creating mysql db using mysql_install_db, then
  2. starting the mysql service then
  3. using mysql_upgrade also we are doing to the server.

在此过程之后,我可以以 root 身份登录,但使用非 root 用户我无法登录服务器:

After this process i can login as root but with a non-root user I am not able to log into the server:

[root@clustertest3 ~]# rpm -qa | grep MySQL
MySQL-client-advanced-5.5.21-1.rhel5
MySQL-server-advanced-5.5.21-1.rhel5


[root@clustertest3 ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1

# Disabling symbolic-links is recommended to prevent assorted security risks;
# to do so, uncomment this line:
# symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[root@clustertest3 ~]# ls -ld /var/lib/mysql/mysql.sock
srwxrwxrwx 1 mysql mysql 0 Nov  30 11:09 /var/lib/mysql/mysql.sock

mysql> CREATE USER 'golden'@'%' IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON * . * TO 'golden'@'%';
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT USER(),CURRENT_USER();
+----------------+----------------+
| USER()         | CURRENT_USER() |
+----------------+----------------+
| root@localhost | root@localhost |
+----------------+----------------+
1 row in set (0.00 sec)

[root@clustertest3 ~]# mysql -ugolden -p
Enter password:
ERROR 1045 (28000): Access denied for user 'golden'@'localhost' (using password: YES)

这是我面临的问题,有什么解决办法吗?

This is the problem I am facing, is there any solution to this?

推荐答案

不要将所有数据库的所有权限授予非 root 用户,这是不安全的(并且您已经拥有具有该角色的root")

Do not grant all privileges over all databases to a non-root user, it is not safe (and you already have "root" with that role)

GRANT <privileges> ON database.* TO 'user'@'localhost' IDENTIFIED BY 'password';

此语句创建一个新用户并授予其选定的权限.即:

This statement creates a new user and grants selected privileges to it. I.E.:

GRANT INSERT, SELECT, DELETE, UPDATE ON database.* TO 'user'@'localhost' IDENTIFIED BY 'password';

查看文档 查看所有权限的详细信息

Take a look at the docs to see all privileges detailed

您可以使用此查询查找更多信息(以root"身份登录):

you can look for more info with this query (log in as "root"):

select Host, User from mysql.user;

看看发生了什么

相关文章