1. 检查mysql
服务是否启动,如果启动,关闭mysql
服务
运行命令:
ps -ef | grep -i mysql
如果mysql服务仍在运行中,就执行关闭mysql服务的的命令:
service mysqld stop
2.修改mysql的配置文件my.conf
一般配置文件基本都在/etc目录下,运行命令:
vi /etc/my.cnf
按 i 编辑文件
在文件的[mysqld]标签下添加一句:skip-grant-tables
然后按ESC 在按 :wq! 保存退出。
3.重启数据库
运行命令:
service mysqld start
4.进入到mysql数据库
运行命令:
mysql -u root
5.修改密码
运行语句:
use mysql;
继续运行语句:
update mysql.user set authentication_string=password('root_password') where user='root';
root_password替换成你想要的密码
例如: 我想要的密码是:123456 就可以修改为下面这样
update mysql.user set authentication_string=password('123456') where user='root';
6.把步骤2加的东西删除掉,在重启服务器,就可以使用刚才修改的密码登录进服务器了。
输入 mysql -u root -p
回车后,输入修改的密码。