Duplicate entry ‘localhost-‘ for key 1

导致这个错误的原因:
因为安装系统时设置了hostname为localhost导致mysql在创建表的时候没有创建成功。因此mysql库中user表里没有root这个用户或者说root没有显示出来,无法查看root的状态。这时需要手动创建。然而在安全模式无法直接创建用户,错误见下。
变个方向就能创建了,先给root给予权限,然后刷新表。之后就可以创建了。
一、killall -TERM mysqld
以安全模式启动mysql
/usr/bin/mysqld_safe –skip-grant-tables &
进入mysql
/usr/bin/mysql
mysql> use mysql
Database changed
mysql> select * from user where user=’root’;
Empty set (0.00 sec)

mysql> grant all privileges on *.* to root@localhost identified by ‘linuxtone’ with GRANT OPTION;
ERROR 1290 (HY000): The MySQL server is running with the –skip-grant-tables option so it cannot execute

this statement
mysql> update user set password=password(‘linuxtone’) where user=’root’;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0

注意单双引号。

mysql> flush privileges;   //注意先刷新下表
Query OK, 0 rows affected (0.00 sec)

mysql> grant all privileges on *.* to root@localhost identified by ‘linuxtone’ with GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)

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

mysql> select * from user where user=’root’;

后期处理:
/usr/local/mysql/bin/mysqladmin shutdown -uroot -p
刚才的密码
然后正常启动:/usr/bin/mysqld_safe &
/usr/local/mysql/bin/mysql -uroot -p
能正常登录!

二Duplicate entry ‘localhost-‘ for key 1 问题,很可能是你的主机名有问题,比如是localhost?
导致mysql 的root 帐号的三个主机值(分别是%、localhost、主机名)的后二者的名字弄成一样了,导使唯一键值出现重复而禁用该记录了?
朋友的主机名设置成了:localhost 遭成了Duplicate entry ‘localhost-‘ for key 1

设置好正确的主机名仍后重新编译就可以了,朋友安装系统的时候没有注意主机名

Tags: ,

1 comment

  1. 换成 MySQL-server-community-5.1.45-1.rhel5.x86_64.rpm
    这个版本就没问题了,Faint!

Leave a Reply

Your email address will not be published.

*