root@bt:~# mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
出现这个错误
root@bt:~# /etc/init.d/mysql status
root@bt:~# /etc/init.d/mysql restart
root@bt:~# mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
mysql> set password for 'root'@'localhost'=password("***************");
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
错误描述: Mysql中添加用户之后可能出现登录时提示ERROR 1045 (28000): Access denied for user的错误.
root@bt:~#/etc/init.d/mysql stop
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
root@bt:~# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
第一步,先使用跳过受权表访问,命令如下:mysqld_safe --user=mysql --skip-grant-tables --skip-networking & (当然,在这之前,先停止mysql服务的运行)。
第二步,mysql -uroot mysql 登录mysql。
第三步,访问mysql数据库下的user表。在我的机器上,mysql> select * from user; 得到的,竟然是
Empty set (0.00 sec)。这说明了,我的mysql没有任何可以访问的用户。知道了这问题所在,解决起来就简单了。
第四步,mysql> INSERT INTO user(host, user, password, select_priv, insert_priv, update_priv) VALUES ('localhost', 'username', PASSWORD(‘yourpassword'), 'Y', 'Y','Y');
Query OK, 1 row affected, 3 warnings (0.00 sec)
返回成功,没问题。嘿嘿~~~就这么简单。
第五步,测试,再重启下mysql服务,正常登录,成功!!!
如果系统表中已存在新建用户,只需重启mysql服务即可。
mysql常用命令补充:
create database 数据库名;
show databases;
use 数据库名;
create user '用户名'@'locallhost' identified by '密码';
flush privileges;
grant select,insert,delete,update on 数据库名.* to 用户名@localhost identified by '密码';
flush privileges;