初次安装MySQL默认没有密码,安装完毕之后最好增加密码
1、命令
1 #/usr/bin/mysqladmin -u root password 'new-password'
格式:mysqladmin -u 用户名 -p 旧密码 password 新密码
2、例子
例1:给root加个密码admin
键入以下命令 :
1 [root@node1]# /usr/bin/mysqladmin -u root password admin
注:因为开始时root没有密码,所以-p旧密码一项就可以省略了。
3、测试是否修改成功
1)不用密码登录
1 [root@node1 /]# mysql
2 ERROR 1045: Access denied for user: 'root@localhost' (Using password: NO)
显示错误,说明密码已经修改。
2)用修改后的密码登录
1 [root@node1 /]# mysql -u root -p 2 Enter password: (输入修改后的密码admin) Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 7
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
MariaDB [(none)]>
成功!
这是通过mysqladmin命令修改口令,也可通过修改库来更改口令。
4)使用SQL语句再次修改密码
1 [root@node1 /]# mysql -u root -p 2 Enter password: (输入修改后的密码admin)
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 28
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
MariaDB [(none)]> 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
MariaDB [mysql]> set password=password('123456');
Query OK, 0 rows affected (0.00 sec)
MariaDB [mysql]> exit
Bye
5)用再次修改后的密码登录
1 [root@node1 /]# mysql -u root -p 2 Enter password: (输入再次修改后的密码)
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 29
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
MariaDB [(none)]>
登录成功!
6)使用linux命令再次修改密码
1 [root@node1 /]# mysqladmin -u root -p password abcdef (这里abcdef是新密码) 2 Enter password: (输入上一次的密码)
7)使用新密码登录
1 [root@node1 /]# mysql -u root -p 2 Enter password: (使用新密码)
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 35
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
MariaDB [(none)]>
登录成功!