我正在运行 Ubuntu 16.04,并遵循这设置/重置 root 密码的指南。但是,如果我在命令行上使用$ mysql -u root -p
或sudo mysql -u root
并输入新密码,我会收到此错误消息:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
如果我正在使用用户和密码从此/etc/mysql/debian.cnf
登录没有任何问题mysql -udebian-sys-maint -p
。
如果我在 mysql 中执行此操作并查询某个主题的
mysql> select User, authentication_string, password_expired, password_last_changed, password_lifetime, account_locked from mysql.user;
+------------------+-------------------------------------------+------------------+-----------------------+-------------------+----------------+
| User | authentication_string | password_expired | password_last_changed | password_lifetime | account_locked |
+------------------+-------------------------------------------+------------------+-----------------------+-------------------+----------------+
| root | *obfuscated_authentication_string======== | N | 2018-11-21 17:02:55 | NULL | N |
| mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | N | 2018-11-21 17:02:48 | NULL | Y |
| mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | N | 2018-11-21 17:02:48 | NULL | Y |
| debian-sys-maint | *obfuscated_authentication_string======== | N | 2018-11-21 17:02:55 | NULL | N |
+------------------+-------------------------------------------+------------------+-----------------------+-------------------+----------------+
我发现 root 的密码自安装以来从未改变过。
如果我遵循这些说明我在步骤 3 之后收到了后台作业的 pid,并且 shell 提示
$ 2019-07-11T14:21:55.218651Z mysqld_safe Logging to syslog.
2019-07-11T14:21:55.226406Z mysqld_safe Logging to '/var/log/mysql/error.log'.
/usr/bin/mysqld_safe: 152: /usr/bin/mysqld_safe: cannot create /var/log/mysql/error.log: Permission denied
2019-07-11T14:21:55.232708Z mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.
/usr/bin/mysqld_safe: 152: /usr/bin/mysqld_safe: cannot create /var/log/mysql/error.log: Permission denied
mysql -u root mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
[1]+ Exit 1 mysqld_safe --skip-grant-tables
当我尝试执行时mysql -u root mysql
。
我需要做什么来清除、设置或重置 root 的密码?
或者是否可以从用户内部实现这一点debian-系统维护?
欢迎任何提示。
答案1
至少我找到了以下解决方案用户和密码从/etc/mysql/debian.cnf
无需启动/停止任何service
也不sudo
:
从标准 shell 登录:
$ mysql -udebian-sys-maint -p
Enter password:
然后里面mysql
:
mysql> use mysql;
mysql> set password for 'root'@'localhost' = password("xXnew_pwXx");
mysql> flush privileges;
mysql> quit;
现在,无需重新启动任何服务,我只需登录即可
$ mysql -u root -p
Enter password:
以及新密码xXnew_pwXx。
现在可以清楚的看到root的密码已经被修改了。
mysql> select User, password_last_changed, password_lifetime, account_locked from mysql.user;
+------------------+-----------------------+-------------------+----------------+
| User | password_last_changed | password_lifetime | account_locked |
+------------------+-----------------------+-------------------+----------------+
| root | 2019-07-11 16:49:52 | NULL | N |
| mysql.session | 2018-11-21 17:02:48 | NULL | Y |
| mysql.sys | 2018-11-21 17:02:48 | NULL | Y |
| debian-sys-maint | 2018-11-21 17:02:55 | NULL | N |
+------------------+-----------------------+-------------------+----------------+
4 rows in set (0,00 sec)
就是这样 ;)