1、连上mysql所在机器
2、停止数据库
[root@lin-21-36-24 ~]# /etc/init.d/mysqld stop
Shutting down MySQL....2019-03-21T11:44:27.298768Z mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
SUCCESS!
3、进入无密码模式
[root@lin-21-36-24 ~]# cd /usr/local/mysql/bin
[root@lin-21-36-24 bin]# sudo su
[root@lin-21-36-24 bin]# ./mysqld_safe --skip-grant-tables &
4、重新开启一个终端窗口
5、在新开终端窗口配置短命令
[root@lin-21-36-24 ~]# alias mysql=/usr/local/mysql/bin/mysql
6、进入mysql命令模式
[root@lin-21-36-24 ~]# mysql
7、进入mysql数据库
mysql> 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
8、获取权限
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
9、重置root用户密码
mysql> update user set authentication_string=password('root') where user='root';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 2 Changed: 0 Warnings: 1
10、退出mysql模式
mysql> exit
11、停止mysql
[root@lin-21-36-24 ~]# /etc/init.d/mysqld stop
Shutting down MySQL.... SUCCESS!
12、启动mysql
[root@lin-21-36-24 ~]# service mysqld start
Starting MySQL. SUCCESS!
PS :如果遇到改完密码后连接被拒
解决方案:
1、在/etc/my.cnf添加配置项
[mysqld]---在这个下面添加skip-grant-tables
2、停止mysql
/etc/init.d/mysqld stop
3、重启mysql
service mysqld start
PS如果遇到mysql stop失败报[ERROR] InnoDB: Unable to lock ./ibdata1, error: 11
1)查看进程被那谁占用然后kill掉ps aux |grep mysql*
[root@lin-19-73-195 ~]# ps aux |grep mysql*
mysql 3221 0.7 0.1 1028464 179972 pts/1 Sl+ 10:55 0:00 mysqld stop
root 3268 0.0 0.0 12132 656 pts/0 S+ 10:57 0:00 grep mysql*
[root@lin-19-73-195 ~]# kill -9 3221
-bash: kill: (3221) - No such process
[root@lin-19-73-195 ~]# ps aux |grep mysql*
root 3270 0.0 0.0 12132 656 pts/0 S+ 10:57 0:00 grep mysql*