mysql密碼遺忘和登錄報錯問題

 

mysql登陸密碼忘記,其實解決辦法很簡單,只須要在mysql的主配置文件my.cnf裏添加一行「跳過受權表」的參數選擇便可!mysql

在my.cnf中添加下面一行:
[root@test-huanqiu ~]# vim /etc/my.cnf              //在[mysqld]區域裏添加
........
skip-grant-tables                       //跳過受權表linux

而後重啓mysql服務,便可無密碼登陸
[root@test-huanqiu ~]# /etc/init.d/mysqld restartsql

登陸後重置密碼
[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)vim

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: 0centos

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)socket

mysql> ui

再次將my.cnf裏添加的那一行註釋,而後重啓mysql
[root@test-huanqiu ~]# vim /etc/my.cnf
........
#skip-grant-tablesspa

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

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

-----------------------------------------------------------------------------------------------------------------------
發現的一個坑:
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=33077)排查用戶和密碼問題, 其實用戶和密碼的錯誤,不會出現111的,因此排除用戶密碼問題ERROR 1045 (28000): Access denied for user 'root'@'XXXX' (using password: YES)

相關文章
相關標籤/搜索