我刚刚构建了一个新的 mysql 实例,但无法与我创建的 2 个用户连接。创建后,我连接(没有密码,因为这是第一次登录):
mysql -uroot
连接后我就跑了
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('Password1');
GRANT ALL PRIVILEGES ON *.* TO 'generic_user'@'%' IDENTIFIED BY 'Password2';
GRANT ALL PRIVILEGES ON *.* TO 'db_admin'@'%' IDENTIFIED BY 'Password3';
保护我的 root 用户并尝试为我的 Web 应用程序创建用户并对其进行管理。使用 root
和 Password1
(不是实际密码)连接工作正常,但 generic_user
无法使用 Password2
连接<强>如果我使用密码。如果我没有密码和 generic_user
,我就被授予访问权限。
例如
mysql -ugeneric_user
Welcome to the MySQL monitor. Commands end with ; or \g.
诗歌安全尝试:
mysql -ugeneric_user -p
Enter password:
ERROR 1045 (28000): Access denied for user 'generic_user'@'localhost' (using password: YES)
我正在从 GRANT
命令复制并粘贴密码,以便我知道它是正确的。
我看到 my.cnf
包含凭据,但我的 my.cnf
也没有:
# Percona Server template configuration
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
default-time-zone="America/New_York"
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
...更新...
经过更多的摆弄,我想我正在以不同的用户身份进行连接。
我跑了:
TRUNCATE TABLE mysql.proxies_priv;
FLUSH PRIVILEGES;
在我发现 user()
和 current_user()
将我作为 root
返回之后。
mysql> SELECT USER(),CURRENT_USER();
+----------------+----------------+
| USER() | CURRENT_USER() |
+----------------+----------------+
| root@localhost | root@localhost |
+----------------+----------------+
1 row in set (0.00 sec)
现在我得到:
+------------------------+----------------+
| USER() | CURRENT_USER() |
+------------------------+----------------+
| generic_user@localhost | @localhost |
+------------------------+----------------+
最佳答案
答案是设置了一个通用用户帐户。我遵循了这个答案,https://dba.stackexchange.com/questions/10852/mysql-error-access-denied-for-user-alocalhost-using-password-yes/10897#10897 ,还有一些额外的部分。
验证我有一个通用帐户后
SELECT user,host,password FROM mysql.user WHERE user='' AND host='localhost';
我删除了,但仍然可以访问,因为我没有刷新权限,所以完整的命令应该是:
delete FROM mysql.user WHERE user='' AND host='localhost';
然后在外壳中
mysqladmin flush-privileges -uroot -p
mysqladmin reload -uroot -p
service mysql restart
我认为没有必要重新启动。
关于Mysql无密码登录、有密码被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49057693/