为MySQL开启root用户的密码

     0评论

最近版本的Ubunut(17.10/18.04)安装MySQL后,root用户默认是没有密码的,本文介绍如何为root用户开启密码。

后文会提供更改的全过程的终端输出,这里先提供一个精简版:

# 无密码登录 sudo mysql -u root # 切换数据库 USE mysql; # 更改认证插件 UPDATE user SET plugin='mysql_native_password' WHERE User='root'; # 刷新权限 FLUSH PRIVILEGES; exit; # 重启服务 sudo systemctl restart mysql.service # 设置密码,Y/N选择均选Y sudo mysql_secure_installation # 测试密码登录 sudo mysql -u root -p

包含终端输出的全过程:

# 不用密码直接可以登录 root@topvps:~# mysql -uroot Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 141 Server version: 5.7.26-0ubuntu0.18.04.1 (Ubuntu) Copyright (c) 2000, 2019, 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数据表 mysql> use mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed # 新版本的MySQL默认是通过auth_socket插件验证用户身份的,不把这个改掉,我们旧无法启用root密码 mysql> select plugin from user WHERE User='root'; +-------------+ | plugin | +-------------+ | auth_socket | +-------------+ 1 row in set (0.00 sec) # 将其更改为mysql_native_password mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='root'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 # 更改完成后刷新权限并退出 mysql> flush privileges; Query OK, 0 rows affected (0.01 sec) mysql> exit; Bye # 重启MySQL服务 root@topvps:~# systemctl restart mysql.service # 执行mysql_secure_installation为MySQL设定密码 root@topvps:~# 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? Press y|Y for Yes, any other key for No: Please set the password for root here. 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. 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! # 再次尝试不用密码登录,会报错误 root@topvps:~# mysql -uroot ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) # 尝试用刚才设置的密码登录,成功 root@topvps:~# 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.26-0ubuntu0.18.04.1 (Ubuntu) Copyright (c) 2000, 2019, 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> quit; Bye

参考资料

-- EOF --

本文最后修改于5年前 (2019-06-04)

差评不太行一般挺好非常不错 (No Ratings Yet)
读取中...
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址