技术积累:MySQL中的权限报错:
在 fedora中安装mysql之后,启动mysqld服务。然后使用mysql监视器进行联机的时候,一直报错,保证错信息如下所示:
[zyp@iZ0jl9sgt76d4kh58mvinxZ ~]$ mysql
ERROR 1045 (28000): Access denied for user 'zyp'@'localhost' (using password: NO)
整体的思路:
1. 打开并编辑/etc/my.cnf或/etc/mysql/my.cnf,具体取决于您的发行版。
2. 添加skip-grant-tables下[mysqld]
3. 重启MySQL
4. 您现在应该可以使用以下命令登录 MySQLmysql -u root -p
5. 跑步mysql> flush privileges;
6. 设置新密码ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassword';
7. 返回/etc/my.cnf 并删除/注释skip-grant-tables
8. 重启MySQL
9. 现在您将可以使用新密码登录mysql -u root -p
具体的实现:
在这里提供的一种解决方案是,在数据库的my.cnf
文件中添加一句:skip-grant-tables
来跳过权限的检测。
对应的文件的位置在如下所示的地方:
/etc/my.cnf
对应的文件中的内容为:
[zyp@iZ0jl9sgt76d4kh58mvinxZ ~]$ cat /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[mysqld]
skip-grant-tables
然后在进行登陆的时候就不会出现刚才的报错了;
如下面的代码所示:
[zyp@iZ0jl9sgt76d4kh58mvinxZ ~]$ sudo systemctl restart mysqld.service
[zyp@iZ0jl9sgt76d4kh58mvinxZ ~]$ mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 8.0.35 Source distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> flush privileges;
Query OK, 0 rows affected (0.008 sec)
MySQL [(none)]> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Rootzyp666'
-> ;
Query OK, 0 rows affected (0.007 sec)
MySQL [(none)]> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Rootzyp666';
Query OK, 0 rows affected (0.007 sec)
MySQL [(none)]> exit
Bye
然后,在重新编辑之前在my.cnf
中的那句话skip-grant-tables
去掉,然后重新加载mysql
,使用systemctl restart mysqld
。
[zyp@iZ0jl9sgt76d4kh58mvinxZ ~]$ sudo vim /etc/my.cnf
" Press ? for help | 1 #
| 2 # This group is read both both by the client and the server
.. (up a dir) | 3 # use it for options that affect everything
/home/zyp/ | 4 #
mysql-community-release-el7-5| 5 [client-server]
~ | 6 #
~ | 7 # include all files from the config directory
~ | 8 #
~ | 9 !includedir /etc/my.cnf.d
~ | 10 [mysqld]
~ | 11 # skip-grant-tables
~ |~
~ |~
~ |~
~ |~
/home/zyp /etc/my.cnf 11,1 All
:wq
[zyp@iZ0jl9sgt76d4kh58mvinxZ ~]$ sudo systemctl restart mysqld.service
[zyp@iZ0jl9sgt76d4kh58mvinxZ ~]$
[zyp@iZ0jl9sgt76d4kh58mvinxZ ~]$
[zyp@iZ0jl9sgt76d4kh58mvinxZ ~]$
[zyp@iZ0jl9sgt76d4kh58mvinxZ ~]$ mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.35 Source distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]>
MySQL [(none)]>
MySQL [(none)]>
MySQL [(none)]>
MySQL [(none)]> status
--------------
mysql Ver 15.1 Distrib 10.5.22-MariaDB, for Linux (x86_64) using EditLine wrapper
Connection id: 8
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server: MySQL
Server version: 8.0.35 Source distribution
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8mb4
Db characterset: utf8mb4
Client characterset: utf8mb3
Conn. characterset: utf8mb3
UNIX socket: /var/lib/mysql/mysql.sock
Uptime: 25 sec
Threads: 2 Questions: 5 Slow queries: 0 Opens: 119 Flush tables: 3 Open tables: 38 Queries per second avg: 0.200
--------------
MySQL [(none)]>
MySQL [(none)]>
MySQL [(none)]>
MySQL [(none)]>
MySQL [(none)]> exit;
Bye
[zyp@iZ0jl9sgt76d4kh58mvinxZ ~]$
最后,我们发现。已经可以使用密码进行登录了。