1.设置数据库密码
[root@db01 ~]# mysqladmin -uroot password '123'
2.使用密码登录数据库
#1.正确的登录方式
[root@db01 ~]# mysql -uroot -p123
[root@db01 ~]# mysql -u root -p123
#2.错误的登录方式
[root@db01 ~]# mysql -u root -p 123
Enter password:
ERROR 1049 (42000): Unknown database '123'
如果使用-p或者--password参数,那么-p或--password和密码之间不能有空格
#3.规范的登录方式
[root@db01 ~]# mysql -u root -p
Enter password:
3.查看用户
mysql> select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| | db01 |
| root | db01 |
| | localhost |
| root | localhost |
+------+-----------+
6 rows in set (0.00 sec)
4.用户的安全优化
#删除没有用的用户
mysql> drop user 'root'@'::1';
Query OK, 0 rows affected (0.00 sec)
mysql> drop user ''@'db01';
Query OK, 0 rows affected (0.00 sec)
mysql> drop user ''@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | db01 |
| root | localhost |
+------+-----------+
3 rows in set (0.00 sec)