[root@SHB-L0120796 bin]# mysql -u root -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

命令行登陆mysql数据库,如果有如上报错,可以my.cnf文件中插入skip-grant-tables;用于跳过权限验证

 

解决办法1:

1. 修改my.cnf文件

进入mysql的安装目录(根据个人mysql实际安装目录为准)

/usr/local/mysql

修改my.cnf,在[mysqld]后面插入skip-grant-tables

[mysqld]
socket=/usr/local/mysql/mysql.sock
# set mysql server port
port = 3306
# set mysql install base dir
basedir=/usr/local/mysql
# set the data store dir
datadir=/usr/local/mysql/data
# set the number of allow max connnection
max_connections=200
# set server charactre default encoding
character-set-server=utf8
# the storage engine
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
explicit_defaults_for_timestamp=true

bind-address = 0.0.0.0
skip-grant-tables

[mysql.server]
user=mysql
basedir=/usr/local/mysql

2. 保存并重启生效: service mysqld restart

service mysqld restart

3. 命令行登陆验证:mysql -u root -p

# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.27 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> 

4. 添加密码: 修改用户密码后执行 quit; 命令

mysql> update user set authentication_string=password('123456') where user='root';

5. 再次登陆验证可用

[root@SHB-L0120796 bin]# mysql -u root -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.27 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> quit;
Bye
[root@SHB-L0120796 bin]# mysql -u mysql -pHuawei123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.27 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> 

 

 

解决办法2:在不修改my.cnf文件的情况下,执行如下命令行

./mysqld --skip-grant-tables --skip-networking --user=root &

 

 

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐