指定skip-grant-tables仍然需要密码登陆的解决办法

在mysqld启动时指定--skip-grant-tables,可以无需密码登陆数据库。这个参数在忘记密码时很有用。

但是我在测试这个参数时,仍然需要我输入密码,这个问题困扰我很久

无论我把skip-grant-tables写在命令行,如

mysqld --defaults-file=/etc/my3307.cnf --skip-grant-tables &

或者写在cnf配置文件中,如

cat /etc/my3307.cnf |grep skip
skip-grant-tables

用 mysql -P3307 -uroot -p去登陆数据库还是会提示要密码

解决办法:

很简单,指定-h127.0.0.1 

比如:mysql -P3307 -uroot -p -h127.0.0.1

我的测试过程:

在cnf文件中写入skip-grant-tables

[root@lzl ~]# cat /etc/my3307.cnf |grep skip
skip-grant-tables

[root@lzl ~]# ps -ef|grep 3307
mysql     3054  3014  2 17:45 pts/2    00:00:00 mysqld --defaults-file=/etc/my3307.cnf
root      3107  3085  0 17:46 pts/3    00:00:00 grep 3307
[root@lzl ~]# mysql -P3307 -uroot -p   --不指定-h会要密码
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)


[root@lzl ~]# mysql -P3307 -uroot -p -h127.0.0.1 指定host不需要密码
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

测试用--skip-grant-tables启动mysqld
[root@lzl ~]# mysqld --defaults-file=/etc/my3307.cnf --skip-grant-tables &

[root@lzl ~]# mysql -P3307 -uroot -p   --不使用-h需要密码
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@lzl ~]# mysql -P3307 -uroot -p -h127.0.0.1   --使用-h不需要密码
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

实际上不指定-S $socket的话,会默认去找/tmp/mysql.sock,这个文件是我3306端口的socket,mysql尝试连接了3306,并没有连接3307。

-S /tmp/my3307.sock可以指定我的3307的socket,即便是这样,还是需要密码的。

总结:

skip-grant-tables可以忽略密码登陆mysql实例,启动mysql时指定

2 跟mysql登陆模式相关,mysql有两种登陆模式socket和TCP/IP。(登陆模式知识点参考我上一篇博客https://blog.csdn.net/qq_40687433/article/details/107771142

3 如果--skip-grant-tables启动mysqld,指定-h时会使用TCP/IP登陆,不需要密码。不指定-h时,通过socket登陆,需要提供密码


 

猜你喜欢

转载自blog.csdn.net/qq_40687433/article/details/107793535