昨天在centos下安装mysql5.7后,却发现无法登陆,才新安装的mysql,初始密码应该没有,但是无论如何都无法登陆(提示密码错误),没办法,只好强行修改mysql密码。
输入
[root@localhost bin]# mysqld_safe --skip-grant-tables &
这句话相当于进入mysql的安全模式。
输入上面这条命令可能会出现错误如下:
[3] 7135
[2] Exit 127 mysqld_stae --skip-grant-tables
[root@localhost bin]# Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
grep: write error: Broken pipe
2016-04-02T10:16:02.306271Z mysqld_safe Logging to '/opt/mysql/data/localhost.localdomain.err'.
2016-04-02T10:16:02.315564Z mysqld_safe The file /usr/local/mysql/bin/mysqld
does not exist or is not executable. Please cd to the mysql installation
directory and restart this script from there as follows:
./bin/mysqld_safe&
See http://dev.mysql.com/doc/mysql/en/mysqld-safe.html for more information
[3]+ Exit 1 mysqld_safe --skip-grant-tables
根据错误提示在/usr/local下创建文件夹 mysql/bin/mysqld
创建好文件夹后,准备开启mysql,但是在这之前先检查一下mysql是否开启
[root@localhost ~]# ps -ef|grep mysqld
如果开启使用kill -15 [pid]杀死mysql。如下:
[root@localhost ~]# ps -ef|grep mysqld
root 6946 1 0 18:15 ? 00:00:00 /bin/sh /opt/mysql/bin/mysqld_safe --datadir=/opt/mysql/data --pid-file=/opt/mysql/data/localhost.localdomain.pid
mysql 7104 6946 0 18:15 ? 00:00:00 /opt/mysql/bin/mysqld --basedir=/opt/mysql --datadir=/opt/mysql/data --plugin-dir=/opt/mysql/lib/plugin --user=mysql --log-error=/opt/mysql/data/localhost.localdomain.err --pid-file=/opt/mysql/data/localhost.localdomain.pid --port=3306
root 7558 7542 0 18:22 pts/0 00:00:00 grep mysqld
[root@localhost ~]# kill -15 7104
再次使用 ps -ef|grep mysqld 确认mysql已经被杀死
开启mysql,如下:
[root@localhost ~]# mysqld_safe --skip-grant-tables &
[1] 7566
[root@localhost ~]# 2016-04-02T10:23:22.463266Z mysqld_safe Logging to '/opt/mysql/data/localhost.localdomain.err'.
2016-04-02T10:23:22.514395Z mysqld_safe Starting mysqld daemon with databases from /opt/mysql/data
mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.11 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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密码: 注意!以前的password字段,在mysql5.7已经被改为了authentication_string。在password()函数里面输入你的新密码,记得加单引号哦。
mysql> UPDATE mysql.user SET authentication_string=password('YourPassword') WHERE User='root' AND Host='localhost';
好了,测试一下用新密码能不能进入:
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.11
Copyright (c) 2000, 2016, 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>
新密码修改成功!
一定要注意以前的password变成了authentication_string