Mysql 5.7 无法登陆、创建数据库的解决办法

新安装完并启动 Mysql 5.7 后,你会发现无法直接使用 Mysql 命令进入,原因是 5.7 版本已经为你生成了默认的 root 用户密码,该密码可以在 /var/log/mysqld.log 里找到或通过以下命令:

grep -i temporary /var/log/mysqld.log

可以看到类似以下:

A temporary password is generated for root@localhost: 6PmM/L,QiJ;B

找到密码就可以登录 Mysql 了

登陆 Mysql 后,发现无法查询数据库,无法创建数据库等操作。这个时候需要你修改 root 的登陆密码,而且要注意的是 Mysql 5.7 版本对密码的安全要求很高,如果使用安全度过低的密码有可能修改失败。修改密码参考如下:

[root@imweb ~]# mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root:

The existing password for the user account root has expired. Please set a new password.

New password:

Re-enter new password:
... Failed! Error: Your password does not satisfy the current policy requirements

New password:

Re-enter new password:
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.

Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y

New password:

Re-enter new password:

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.

Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- Dropping test database...
Success.

- Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

或者使用以下命令来修改这里同样需要注意密码的安全度要够强:

SET PASSWORD FOR ‘root’@’localhost’ = PASSWORD(‘Yourpassword’);

修改完密码就可以正常操作 Mysql 了!

安装 Mysql 5.7 版本,请参考:Centos Linux 安装最新版本 mysql 5.7

相关文章