Specify --skip-grant-tables when mysqld starts, you can log in to the database without a password. This parameter is useful when the password is forgotten.
But when I test this parameter, I still need to enter the password. This problem has bothered me for a long time.
No matter if I write skip-grant-tables on the command line, such as
mysqld --defaults-file=/etc/my3307.cnf --skip-grant-tables &
Or write in the cnf configuration file, such as
cat /etc/my3307.cnf |grep skip
skip-grant-tables
Use mysql -P3307 -uroot -p to log in to the database or you will be prompted for a password
Solution:
Very simple, specify -h127.0.0.1
For example: mysql -P3307 -uroot -p -h127.0.0.1
My testing process:
Write skip-grant-tables in the cnf file
[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 --If you don't specify -h, you will need a password
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>
Test start mysqld with --skip-grant-tables
[root@lzl ~]# mysqld --defaults-file=/etc/my3307.cnf --skip-grant-tables &
[root@lzl ~]# mysql -P3307 -uroot -p --Do not use -h requires a password
Enter password:
ERROR 1045 (28000): Access denied for user'root '@'localhost' (using password: NO)
[root @lzl ~]# mysql -P3307 -uroot -p -h127.0.0.1 --Use -h without a password
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>
In fact, if you don't specify -S $socket, it will find /tmp/mysql.sock by default. This file is my socket on port 3306. MySQL tried to connect to 3306 but failed to connect to 3307.
-S /tmp/my3307.sock can specify the socket of my 3307, even so, a password is still required.
to sum up:
1 skip-grant-tables can ignore the password to log in to the mysql instance, specified when starting mysql
2 Related to mysql login mode, mysql has two login modes socket and TCP/IP. (For the knowledge points of login mode, please refer to my previous blog https://blog.csdn.net/qq_40687433/article/details/107771142 )
3 If --skip-grant-tables starts mysqld, when -h is specified, TCP/IP login will be used, and no password is required. When -h is not specified, login via socket, password is required