MySQL5.7安装后设置账号密码

新版的MySQL安装过程中不会再让用户配置账号密码等其他相关信息,导致安装完成后没有无法登录的情况。

使用mysql命令“mysql -u root”登录会报错:

ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: NO)

然后你用“mysql – u root -p”还会报错

ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: YES)

下面我们就详细说明一下,采用Ubuntu默认源安装的MySQL Server配置登录管理员账号的方法。

教程环境说明

  • 服务器版本:Ubuntu 18.4.1 LTS
  • MySQL:5.7
  • 环境:本地vm搭建

具体操作

(此处只讲MYSQL安装,如果需要Apache、PHP等lamp安装,请看本站其他文章)安装MySQL方法:

sudo apt install mysql-server

随后执行:

sudo mysql_secure_installation 

注意一定要用sudo命令。

danny@ubuntuserver1804:~$ sudo mysql_secure_installation
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
#询问你是否需要安装密码验证插件,此处我选择no
Press y|Y for Yes, any other key for No: n
Please set the password for root here.
#随即让你设置root新密码,输入两遍
New password:
Re-enter new password:
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.
#询问是否删除匿名用户。强烈推荐删除y
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.

#接着,询问你是否需要关闭远程连接。一般使用场景中都会关闭。除非你在两个服务器直接公用一个MySQL。此处建议输入y
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.
#询问是否重新加载,输入y
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!

到此,完成了MySQL的安全配置。但是,你直接用命令mysql -u root -p还是会提示登录错误ERROR 1698 (28000): Access denied for user ‘root’@’localhost’。如下,

danny@ubuntuserver1804:~$ mysql -u root -p
Enter password:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

看好下一步,其实很简单,只要加上sudo命令就可以了

danny@ubuntuserver1804:~$ sudo mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.29-0ubuntu0.18.04.1 (Ubuntu)
Copyright (c) 2000, 2020, 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> 

虽然可以登录了,但是如果安装Wordpress等系统时还是不能使用。为什么呢?那是因为在数据库中,还是不允许账号密码登录。我们紧接着在mysql命令下输入

mysql>SELECT user,authentication_string,plugin,host FROM mysql.user;

可以看到root只能socket方式登录

那我们用update更新它:

UPDATE mysql.user SET plugin="mysql_native_password", authentication_string=PASSWORD("你的新密码") WHERE user="root";

此处一定要记得改plugin的值为 mysql_native_password 

最后还要刷新一下权限。

mysql>FLUSH PRIVILEGES; 

OK。就此完全搞定。就此,你可以安装Wordpress等开源系统了。

文章版本:

初始版本 1.0

发表评论

电子邮件地址不会被公开。 必填项已用*标注