mysql设置root密码的问题?

cheerduo
  • 9

背景:

使用root权限登录服务器,安装mysql,使用mysqladmin给用户名为"root"的user设置密码为xxx

问题:

  • 在原root账号登录服务器的情况下,然后直接使用mysql命令不用输密码即可登录mysql;

  • 使用用户名为test的私钥登录服务器,有以下几种情况:

   1. 直接使用mysql命令提示Access denied for user 'test'@'localhost
   2. 而使用mysql -uroot -pxxx则提示Access denied for user 'root'@'localhost
   3. 另外,使用sudo mysql则成功且不需要输mysql密码

期待结果:

mysql只有一个用户User="root"的情况下,任何账号登录服务器,均需要输入密码才能进入mysql,且不要加sudo,即类似mysql -uroot -pxxx这样,可以吗?

回复
阅读 1.7k
2 个回答

1.root用户登录OS:

mysql> select user,host from mysql.user;
user host
root localhost

1 row in set (0.00 sec)

mysql> exit
Bye
[root@mysql3 ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 20
Server version: 5.6.41-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> select user();
user()
root@localhost

1 row in set (0.00 sec)

mysql> exit
Bye

2.sam用户登录OS:
[sam@mysql3 ~]$ mysql
ERROR 1045 (28000): Access denied for user 'sam'@'localhost' (using password: YES)
[sam@mysql3 ~]$ mysql -uroot -psam123
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 22
Server version: 5.6.41-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> select user();
user()
root@localhost

1 row in set (0.00 sec)

你要的是这个?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏