After mysql is installed, you cannot log on to the mysql shell, that is, mysql & gt; encountered: ERROR 1045 (28000): Access denied for user & #39; root & #39; @ & #39; localhost ',

Source: Internet
Author: User
Tags mysql command line

After mysql is installed, you cannot log on to the mysql shell, that is, mysql.> the following ERROR occurs: ERROR 1045 (28000): Access denied for user 'root' @ 'localhost ',

[Root @ hzswtb2-mpc ~] # Mysql
ERROR 1045 (28000): Access denied for user 'root' @ 'localhost' (using password: NO)

ERROR 1045 (28000): Access denied for user 'root' @ 'localhost' (using password: NO)

I found a lot of articles on the Internet and still did not solve my problem;

1. the keyword of the mysql command line SQL command is case-insensitive. in mysql 5.1.51;

2. stop the normally started mysqld first;

3. Start mysqld by bypassing the root password check;

 

The solution is as follows:

1. stop mysqld

[Root @ hzswtb2-mpc ~] # Service mysqld stop
Stopping mysqld: [OK]

2. Run the mysql server in the background by bypassing the root password check;

[Root @ hzswtb2-mpc ~] # Mysqld_safe -- user = mysql -- skip-grant-tables -- skip-networking &
[1] 25513
[Root @ hzswtb2-mpc ~] #141215 15:00:16 mysqld_safe Logging to '/var/log/mysqld. log '.
141215 15:00:16 mysqld_safe Starting mysqld daemon with databases from/var/lib/mysql

3. Press enter from mysql to enter mysql shell. Follow the methods left by the predecessors to update the password of the root user in the user table. The result is 0 rows affected, and then select to check whether the user table is empty;

[Root @ hzswtb2-mpc ~] # Mysql
Welcome to the MySQL monitor. Commands end with; or \ g.
Your MySQL connection id is 1
Server version: 5.1.51 Source distribution

Mysql> UPDATE user set password = PASSWORD ('000000') where USER = 'root ';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0

Mysql> use mysql
Database changed
Mysql> show tables;
+ --------------------------- +
| Tables_in_mysql |
+ --------------------------- +
| Columns_priv |
| Db |
| Event |
| Func |
| General_log |
| Help_category |
| Help_keyword |
| Help_relation |
| Help_topic |
| Host |
| Ndb_binlog_index |
| Plugin |
| Proc |
| Procs_priv |
| Servers |
| Slow_log |
| Tables_priv |
| Time_zone |
| Time_zone_leap_second |
| Time_zone_name |
| Time_zone_transition |
| Time_zone_transition_type |
| User |
+ --------------------------- +
23 rows in set (0.00 sec)

Mysql> select * from user;
Empty set (0.00 sec)

4. insert a user named root in the user table;

Mysql> insert into user (Host, User, Password, Select_priv, Insert_priv) VALUES ('localhost', 'root', PASSWORD ('pass3'), 'y ', 'y ');
Query OK, 1 row affected, 3 warnings (0.00 sec)

5. A root user is displayed;

Mysql> select * from user
->;
+ ----------- + ------ + Response + ----------- + response + -------------- + ----------- + ------------ + response + ------------ + -------------- + response ----------------------- + pipeline + -------------- + pipeline + ------------ + -------------- + ---------- + ------------ + ------------- + ------------ + pipeline + ------------- + pipeline +
| Host | User | Password | Select_priv | primary | Update_priv | Delete_priv | Create_priv | Drop_priv | primary | File_priv | Grant_priv | primary | Index_priv | Alter_priv | primary | Super_priv | | response | Execute_priv | response | Create_user_priv | Event_priv | response | ssl_type | ssl_cipher | response | max_updates | max_connections | max_user_connections |
+ ----------- + ------ + Response + ----------- + response + -------------- + ----------- + ------------ + response + ------------ + -------------- + response ----------------------- + pipeline + -------------- + pipeline + ------------ + -------------- + ---------- + ------------ + ------------- + ------------ + pipeline + ------------- + pipeline +
| Localhost | root | * 35B5E90BC4F5AE5D02ED515DF6B61141F24EDA02 | Y | N | N | 0 | 0 | 0 | 0 |
+ ----------- + ------ + Response + ----------- + response + -------------- + ----------- + ------------ + response + ------------ + -------------- + response ----------------------- + pipeline + -------------- + pipeline + ------------ + -------------- + ---------- + ------------ + ------------- + ------------ + pipeline + ------------- + pipeline +
1 row in set (0.00 sec)

6. If the password of the root user is updated, the success is returned;


Mysql> UPDATE user SET Password = PASSWORD ('newpass') WHERE user = 'root ';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

Mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

Mysql> exit

7. stop the mysql service and restart the mysql server in normal mode. the mysql-u root-p password is newpass.

[Root @ hzswtb2-mpc ~] # Service mysqld stop
141215 16:12:24 mysqld_safe mysqld from pid file/var/run/mysqld. pid ended
Stopping mysqld: [OK]
[1] + Done mysqld_safe -- user = mysql -- skip-grant-tables -- skip-networking
[Root @ hzswtb2-mpc ~] # Service mysqld start
Starting mysqld: [OK]
[Root @ hzswtb2-mpc ~] #
[Root @ hzswtb2-mpc ~] # Mysql
ERROR 1045 (28000): Access denied for user 'root' @ 'localhost' (using password: NO)
[Root @ hzswtb2-mpc ~] # Mysql-u root-p
Enter password:
Welcome to the MySQL monitor. Commands end with; or \ g.
Your MySQL connection id is 4
Server version: 5.1.51 Source distribution

Copyright (c) 2000,201 0, Oracle and/or its affiliates. All rights reserved.
This software comes with absolutely no warranty. This is free software,
And you are welcome to modify and redistribute it under the GPL v2 license

Type 'help; 'or' \ H' for help. type' \ C' to clear the current input statement.

Mysql>

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.