## mysql默认没有设密码,直接登录
[root@wy ~]# mysql -uroot
## 设置密码
[root@wy ~]# mysqladmin -uroot password '123456'
[root@wy ~]# mysql -uroot -p123456
若忘记root密码???
## 编辑配置文件
[root@wy ~]# vim /etc/my.cnf
[mysqld] #在mysqld模块下添加以下内容
skip-grant
## 重启mysql
[root@wy ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
## 进入mysql
[root@wy ~]# mysql -uroot
## 使用mysql库
mysql> use mysql
Database changed
## 修改user表
mysql> update user set password=password('123456') where user='root';
Query OK, 2 rows affected (0.05 sec)
Rows matched: 3 Changed: 2 Warnings: 0
## 将更改内容写入磁盘,否则保存在内存中不会及时生效
mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)
## 修改后注释掉skip-grant
[root@wy ~]# vim /etc/my.cnf
[mysqld]
#skip-grant
解释说明:
这行必须注释,否则会一直跳过认证过程,非常危险;
# 重启mysql生效
[root@wy ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!