1、拥有原来的myql的root的密码;
mysql -uroot -p
Enter password: 【输入原来的密码】(如果安装完mysql第一次设置root密码,则直接回车)
mysql>use mysql;
mysql> update user set password=password("123456") where user='root';
mysql> flush privileges;
mysql> exit;
注意:有版本差异
update user set password=password(‘123456’) where user=’root’;
update user set authentication_string=password(‘123456’) where user=’root’;(5.7)
(最好在记事本总过滤下样式或者重新打一遍)
2、忘记原来的myql的root的密码;
首先,你必须要有操作系统的root权限了。也就说需要以root的身份登录到操作系统,然后进行一下操作。
(1)编辑MySQL配置文件my.cnf
vi /etc/my.cnf
编辑文件,找到[mysqld],在下面添加一行skip-grant-tables
[mysqld]
skip-grant-tables
:wq! #保存退出
service mysql restart #重启MySQL服务
(2)进入MySQL控制台
mysql -uroot -p
直接按回车,这时不需要输入root密码。
(3)修改root密码
mysql>use mysql;
mysql> update user set password=password(“123456”) where user=’root’;
mysql> flush privileges;刷新权限(必须步骤)
mysql> exit;
注意:有版本差异
update user set password=password(‘123456’) where user=’root’;
update user set authentication_string=password(‘123456’) where user=’root’;(5.7)
(最好在记事本总过滤下样式或者重新打一遍)
(4)取消/etc/my.cnf中的skip-grant-tables
vi /etc/my.cnf
编辑文件,找到[mysqld],删除skip-grant-tables这一行
:wq! #保存退出
(5)重启mysql
service mysql restart
重启mysql,这个时候mysql的root密码已经修改为123456