mysql -uroot -p error reason

1. Analysis of error causes

When executing mysql export today, I found that there was no problem in entering the password normally.

mysqldump -h localhost -uroot -p database > /var/lib/mysql/htSQL/dump0710.sql
#然后再输入密码,正常导出

But I tried again and put the password after -p but got an error. I checked carefully and found that the password was correct. What was the problem? I searched online and didn't get the answer I wanted, so I Here is a summary of the reasons for this situation:

1. The MySQL service is not started.

Make sure that the MySQL database service has been started. This can be solved by restarting the MySQL service or checking the service running status, for example:

#查看mysql启动状态(如果没找到试试这个:service mysqld status)
service mysql status 

#启动mysql(如果没找到试试这个:systemctl start mysqld)
service mysql start

#重启MySQL(如果没找到试试这个:service mysqld restart)
service mysql restart

Insert image description here

2. The MySQL installation path is not added to the environment variable (windows)

The MySQL command may not be found in the console. You can try to enter the bin directory in the MySQL installation directory and then execute the command

3. There are special characters in the password (the reason for my error)

Problems may occur if your password contains some special characters (such as #, $, !, etc.). When using these special characters on the command line, you need to use escape characters or quotation marks. For example, if the password is p@ssw!rd, you can try the following command:

mysql -uroot -p'p@ssw!rd'

4. MySQL configuration file error

If there are errors or incorrect parameter settings in the MySQL configuration file, the login command may not work properly. You can check the settings in the MySQL configuration file (usually my.cnf or my.ini) and make sure they are correct

Guess you like

Origin blog.csdn.net/weixin_52796198/article/details/131637835