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/bin/mysqladmin shutdown -uroot -p
刚才的密码
然后正常启动:/usr/bin/mysqld_safe &
/usr/bin/mysql -uroot -p
能正常登录!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值