mysql在第一次登陆时会提示ERROR 1045 (28000): Access denied for user root@localhost。
现在网上流传的使用mysqladmin或者mysqld_safe命令来启动已经不行了。新版本的mysql已经移除了mysqld_safe, mysqladmin还在,但是仍然无法登陆。
亲测有用的另一种方法:
Step 1 编辑/etc/my.cnf
root@linux: vim /etc/my.cnf
在mysqld项最末尾加上一行 skip-grant-tables
[mysqld]
#
# 省略若干行
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
skip-grant-tables
Step 2 重启mysql服务,修改root用户密码
root@linux: service mysqld restart
root@linux: mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.18 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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 mysql.user set authentication_string=password('newpasswd') where user='root';
mysql> exit;
Step 3 恢复设置, 再次重启mysql服务
删除/etc/my.cnf 最后的skip-grant-tables, 并重启mysql服务。
root@linux: vim /etc/my.cnf
[mysqld]
#
# 省略若干行
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
root@linux: service mysqld restart
大功告成!