ubuntu14.10下解决"ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)"

1. 问题描述
ubuntu下mysql数据库的安装请看:http://blog.csdn.net/cryhelyxx/article/details/23551605
修改mysql数据库用户root的密码请看:http://blog.csdn.net/cryhelyxx/article/details/39475433
好了, 了解了上面两篇文章后, 我们假设你已在ubuntu下成功安装上了mysql数据库。
启动ubuntu14.10系统后, ctrl + alt + t打开终端
首先进入mysql的安装目录下启动mysql服务:
 
  
xx@ubuntu:~$ cd /usr/local/mysql/
xx@ubuntu:/usr/local/mysql$ sudo ./support-files/mysql.server start
Starting MySQL
.. * 
xx@ubuntu:/usr/local/mysql$ 
启动mysql服务后, 接着我们进入mysql的shell, 即进入mysql的控制台,
 
  
xx@ubuntu:/usr/local/mysql$ mysql -u root mysql
ERROR 1045 (28000): Access denied for user 'xx'@'localhost' (using password: NO)
xx@ubuntu:/usr/local/mysql$
发现出现拒绝访问“ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)”信息。
回到上面, 我们在刚刚安装完mysql数据库是未对mysql中的root用户进行密码设置的。下面我们通过修改mysql中的root用户密码进行解决问题。
2. 解决方法
	我们先把mysql的服务停止下来
 
  
xx@ubuntu:/usr/local/mysql$ sudo ./support-files/mysql.server stop
Shutting down MySQL
.. * 
xx@ubuntu:/usr/local/mysql$ 
mysql服务停止后, 执行以下命令, 这里我们sudo -s切换到root管理员下执行命令,如下
 
  
./mysqld_safe --user=mysql --skip-grant-tables --skip-networking& 
 
  
root@ubuntu:/usr/local/mysql# cd bin/
root@ubuntu:/usr/local/mysql/bin# ./mysqld_safe --user=mysql --skip-grant-tables --skip-networking&
[1] 5810
root@ubuntu:/usr/local/mysql/bin# 150104 19:01:43 mysqld_safe Logging to '/usr/local/mysql/data/ubuntu.err'.
150104 19:01:43 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
150104 19:15:35 mysqld_safe mysqld from pid file /usr/local/mysql/data/ubuntu.pid ended
当出现上面的信息时, 我们再ctrl + alt + t打开另一个终端窗口, 键入mysql -u root mysql进入mysql的shell下, 并可以修改mysql里root用户的密码
如下:
 
  
xx@ubuntu:~$ mysql -u root mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.7.5-m15 MySQL Community Server (GPL)

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> UPDATE user SET Password=PASSWORD('123456') whereUSER='root';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'whereUSER='root'' at line 1
mysql> use mysql;
Database changed
mysql> UPDATE user SET password=PASSWORD('123456') WHERE user='root';
Query OK, 1 row affected (0.22 sec)
Rows matched: 1  Changed: 1  Warnings: 0

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

mysql> exit
Bye
xx@ubuntu:~$ mysql -uroot -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 2
Server version: 5.7.5-m15

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> quit
Bye
xx@ubuntu:~$ 
 
  
mysql>use mysql;
mysql>UPDATE user SET password=PASSWORD('newpassword') WHERE user='root';
mysql>FLUSH PRIVILEGES;
即上面三个命令就可完成mysql里root用户的密码修改。
3. 再次以新密码进入mysql的shell
如果mysql未启动, 请先启动mysql服务, 再来执行命令
启动mysql服务如下:
 
  
xx@ubuntu:~$ cd /usr/local/mysql/
xx@ubuntu:/usr/local/mysql$ sudo ./support-files/mysql.server start
Starting MySQL
.. * 
xx@ubuntu:/usr/local/mysql$
 
  
 
  
$ mysql -uroot -p123456
 
  
xx@ubuntu:~$ mysql -uroot -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 2
Server version: 5.7.5-m15 MySQL Community Server (GPL)

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> quit
Bye
xx@ubuntu:~$ mysql -uroot -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.5-m15 MySQL Community Server (GPL)

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> 
上面提示密码显示不安全信息, 我们也可以键入
 
  
$ mysql -uroot -p
然后再接入mysql用户root的新密码进行登录shell即可, 如下:
 
  
xx@ubuntu:~$ mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.5-m15 MySQL Community Server (GPL)

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> 
 
  
 
OK, Enjoy it!!!
 
  
 
 

你可能感兴趣的:(Linux,数据库)