无法访问mysql数据库

时间:2014-10-23 07:39:34

标签: mysql database

我执行了以下命令来更改数据库密码:

mysql> UPDATE mysql.user SET Password=('root') WHERE User = 'root';
Query OK, 4 rows affected (0.06 sec)
Rows matched: 4  Changed: 4  Warnings: 0

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

现在我无法使用密码“root”访问数据库。我收到以下消息:

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

任何人都可以帮我吗?

1 个答案:

答案 0 :(得分:1)

请在Terminal上运行:

  • 停止MySQL守护程序:

    /etc/init.d/mysql stop

  • 安全地启动MySQL:

    sudo /usr/bin/mysqld_safe --skip-grant-tables &

  • 然后您就可以开始重置密码了:

    SET PASSWORD FOR 'root@localhost' = PASSWORD('yourNewPassword');
    FLUSH PRIVILEGES;

修改
我在自己的计算机上一步一步地运行命令。您可以关注它们,它们会成功运行:

begueradj@begueradj-X71Q:/var/www$ sudo /etc/init.d/mysql stop
[sudo] password for begueradj: 
Rather than invoking init scripts through /etc/init.d, use the service(8)
utility, e.g. service mysql stop

Since the script you are attempting to invoke has been converted to an
Upstart job, you may also use the stop(8) utility, e.g. stop mysql
mysql stop/waiting
begueradj@begueradj-X71Q:/var/www$ sudo mysqld_safe --skip-grant-tables &
[2] 6035
begueradj@begueradj-X71Q:/var/www$ 141023 10:11:43 mysqld_safe Can't log to error log and syslog at the same time.  Remove all --log-error configuration options for --syslog to take effect.
141023 10:11:43 mysqld_safe Logging to '/var/log/mysql/error.log'.
141023 10:11:43 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

begueradj@begueradj-X71Q:/var/www$ mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.37-0ubuntu0.12.04.1 (Ubuntu)

Copyright (c) 2000, 2014, 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> update user set password=PASSWORD("begueradj") where User='root';
Query OK, 4 rows affected (0.02 sec)
Rows matched: 4  Changed: 4  Warnings: 0

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

mysql> 

您现在可以退出。 BEGUERADJ。