欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

详解mysql密码遗忘和登陆报错的问题解决

程序员文章站 2022-03-20 22:34:35
...
下面小编就为大家带来一篇浅谈mysql密码遗忘和登陆报错的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

mysql登录密码忘记,其实解决办法很简单,只需要在mysql的主配置文件my.cnf里添加一行“跳过授权表”的参数选择即可!

在my.cnf中添加下面一行:

[root@test-huanqiu ~]# vim /etc/my.cnf            
  //在[mysqld]区域里添加
........
skip-grant-tables 
                     
 //跳过授权表

然后重启mysql服务,即可无密码登录

[root@test-huanqiu ~]# /etc/init.d/mysqld restart

登录后重置密码

[root@test-huanqiu ~]# mysql 
mysql> select host,user,password from mysql.user;
+--------------------+------+-------------------------------------------+
| host | user | password |
+--------------------+------+-------------------------------------------+
| localhost | root | *481ACA1BD6D1E86221244904E9C0FABA33B40B84 |
| host-192-168-1-117 | root | |
| 127.0.0.1 | root | |
| ::1 | root | |
| localhost | | |
| host-192-168-1-117 | | |
+--------------------+------+-------------------------------------------+
6 rows in set (0.00 sec)
mysql> update mysql.user set password=password("123456") where host="localhost" and user="root";
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> select host,user,password from mysql.user;
+--------------------+------+-------------------------------------------+
| host | user | password |
+--------------------+------+-------------------------------------------+
| localhost | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| host-192-168-1-117 | root | |
| 127.0.0.1 | root | |
| ::1 | root | |
| localhost | | |
| host-192-168-1-117 | | |
+--------------------+------+-------------------------------------------+
6 rows in set (0.00 sec)
mysql>

再次将my.cnf里添加的那一行注释,然后重启mysql

[root@test-huanqiu ~]# vim /etc/my.cnf........#skip-grant-tables
[root@test-huanqiu ~]# /etc/init.d/mysqld restart
[root@test-huanqiu ~]# mysql -p123456mysql>

-----------------------------------------------------------------------------------------------------------------------

发现的一个坑:

mysql之前进行了全量备份,在恢复后,发现用之前的密码登陆不进去了!
使用上面的方法,无密码登陆后再重置密码,但是重置密码后发现仍然登陆不进去。

最后发现是因为mysql.user表内容被清空了!

mysql> select host,user,password from user;
Empty set (0.00 sec)

解决:

插入数据,再重置密码

mysql> insert into user(host,user,password) values("localhost","root","123456");
Query OK, 1 row affected, 3 warnings (0.01 sec)

mysql> select host,user,password from user;
+-----------+------+----------+
| host | user | password |
+-----------+------+----------+
| localhost | root | 123456 |
+-----------+------+----------+
1 row in set (0.00 sec)

mysql> update mysql.user set password=password("123456") where host="localhost" and user="root";
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select host,user,password from user;
+-----------+------+-------------------------------------------+
| host | user | password |
+-----------+------+-------------------------------------------+
| localhost | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+------+-------------------------------------------+
1 row in set (0.00 sec)
mysql> insert into user(host,user,password) values("127.0.0.1","root","123456");
Query OK, 1 row affected, 3 warnings (0.00 sec)

mysql> select host,user,password from user;
+-----------+------+-------------------------------------------+
| host | user | password |
+-----------+------+-------------------------------------------+
| localhost | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| 127.0.0.1 | root | 123456 |
+-----------+------+-------------------------------------------+
2 rows in set (0.00 sec)

mysql> update mysql.user set password=password("123456") where user="root";
Query OK, 1 row affected (0.00 sec)
Rows matched: 2 Changed: 1 Warnings: 0

mysql> select host,user,password from user;
+-----------+------+-------------------------------------------+
| host | user | password |
+-----------+------+-------------------------------------------+
| localhost | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| 127.0.0.1 | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+------+-------------------------------------------+

然后使用重置后的密码就能正常登陆了!

------------------------------------------------------------------------------------------------------------------

mysql登录报错1:

[root@test-huanqiu ~]# mysql -p123456
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111)
[root@test-huanqiu ~]# ps -ef|grep mysql
root 28279 1 0 12:55 ? 00:00:00 /bin/sh /usr/local/mysql//bin/mysqld_safe --datadir=/data/mysql/data --pid-file=/data/mysql/data/mysql.pid
mysql 29059 28279 0 12:55 ? 00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql/ --datadir=/data/mysql/data
 --plugin-dir=/usr/local/mysql//lib/plugin --user=mysql --log-error=/data/mysql/data/mysql-error.log --pid-file=/data/mysql/data/mysql.pid 
 --socket=/usr/local/mysql/var/mysql.sock --port=3306
root 30726 11268 0 12:58 pts/2 00:00:00 grep mysql

可知,当前mysql.sock文件路径是/usr/local/mysql/var/mysql.sock,

解决办法:做软链接

[root@test-huanqiu ~]# ll /usr/local/mysql/var/mysql.sock
rwxrwxrwx. 1 mysql mysql 0 Nov 29 12:55 /usr/local/mysql/var/mysql.sock
[root@test-huanqiu ~]# rm -f /var/lib/mysql/mysql.sock
[root@test-huanqiu ~]# ln -s /usr/local/mysql/var/mysql.sock /var/lib/mysql/mysql.sock

这样就没问题了

[root@test-huanqiu ~]# mysql -p123456
mysql>

----------------------------------------------------------------------------------------------------

启动mysql的时候报错:

Starting MySQL.... ERROR! The server quit without updating PID file (/data/mysql/data/mysql.pid).

尝试的解决办法:

(1)权限问题

可能是mysql.pid文件没有写的权限,将mysql的安装目录和数据目录的权限都设置成mysql启动用户权限。比如都修改为mysql:mysql权限

(2)可能进程里已经存在mysql进程

ps -ef|grep mysql 查出要是有mysql进程存在,就kill掉,再尝试重启mysql

(3)可能是多次在机器上安装mysql,有残余数据影响了服务的启动。

去mysql的数据目录看看,如果存在mysql-bin.index,就立刻删掉它,它就是罪魁祸首了!

(4)mysql在启动时没有指定配置文件时会使用/etc/my.cnf配置文件,请打开这个文件查看在[mysqld]节下有没有指定数据目录。

在[mysqld]下添加设置,如datadir = /data/mysql/data

(5)skip-federated字段问题

检查一下my.cnf文件中有没有没被注释掉的skip-federated字段,如果有就立即注释掉。

(6)错误日志目录不存在

去my.cnf文件下是否有log日志配置路径,如果有,查看下日志目录是否存在,日志目录权限要确保是mysql启动用户权限。

(7)selinux惹的祸,如果是centos系统,默认会开启selinux

闭它,打开/etc/selinux/config,把SELINUX=enforcing改为SELINUX=disabled后存盘退出重启机器试试。
(8)重新初始化mysql数据试试

切换到mysql的安装目录下

./scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql/data --user=mysql

--------------------------------

使用mysql服务端授权的信息登录mysql,报错如下:

ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.1.14' (111)

可能的原因有:

1)可能网络连接问题,远程ping 192.168.1.14 ,能ping通,排除此情况;

2)mysql服务端192.168.1.14的iptables里做了3306端口连接的白名单限制;

3)mysql服务端192.168.1.14的my.cnf文件里配置了bind_address地址绑定,不允许本机连接;

4)mysql服务端192.168.1.14的my.cnf文件里配置了skip_networking,这使用MySQL只能通过本机Socket连接(socket连接也是本地连接的默认方式),放弃对TCP/IP的监听;

5)排查DNS解析问题,检查mysql服务端192.168.1.14的my.cnf文件里是否设置了skip_name_resolve。这个参数加上后,不支持主机名的连接方式。

6)排查--port问题,有可能mysql服务端192.168.1.14的MySQL port不是默认3306,比如是3307端口,这样,远程连接的时候要加上--port=3307

7)排查用户和密码问题, 其实用户和密码的错误,不会出现111的,所以排除用户密码问题
ERROR 1045 (28000): Access denied for user 'root'@'XXXX' (using password: YES)

以上就是详解mysql密码遗忘和登陆报错的问题解决的详细内容,更多请关注其它相关文章!