mysql socket文件丢失处理或者mysql.sock被删除

为了数据库安全,限制了MySQL中root用户只能本地登录,其他用户不得拥有授权权限。结果悲剧了,登录到一台服务器上发现unix_socket文件不见了。root用户无法登陆。

怎办?情急之下请出DBA用户伪造一个root@127.0.0.1,原地复活。

[plain]  view plain  copy
  1. <span style="font-size:18px;color:#000099;">mysql> select user,host,password from user;  
  2. +----------+-----------+-------------------------------------------+  
  3. | user     | host      | password                                  |  
  4. +----------+-----------+-------------------------------------------+  
  5. | root     | localhost | *548E9BC80642113DD04156ED194459C8EAC2A08E |  
  6. | xm_dba   | 192.168.% | *548E9BC80642113DD04156ED194459C8EAC2A08E |  
  7. | rep_user | 192.168.% | *548E9BC80642113DD04156ED194459C8EAC2A08E |  
  8. +----------+-----------+-------------------------------------------+  
  9. 3 rows in set (0.00 sec)  
  10.   
  11. mysql> exit  
  12. Bye  
  13. [root@localhost data]# mysql -uroot -p -hlocalhost -P3306   
  14. Enter password:   
  15. Welcome to the MySQL monitor.  Commands end with ; or \g.  
  16. Your MySQL connection id is 4  
  17. Server version: 5.5.18-log Source distribution  
  18.   
  19. Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.  
  20.   
  21. Oracle is a registered trademark of Oracle Corporation and/or its  
  22. affiliates. Other names may be trademarks of their respective  
  23. owners.  
  24.   
  25. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.  
  26.   
  27. mysql> \s  
  28. --------------  
  29. mysql  Ver 14.14 Distrib 5.5.18, for Linux (x86_64) using readline 5.1  
  30.   
  31. Connection id:          4  
  32. Current database:  
  33. Current user:           root@localhost  
  34. SSL:                    Not in use  
  35. Current pager:          stdout  
  36. Using outfile:          ''  
  37. Using delimiter:        ;  
  38. Server version:         5.5.18-log Source distribution  
  39. Protocol version:       10  
  40. Connection:             Localhost via UNIX socket  
  41. Server characterset:    utf8  
  42. Db     characterset:    utf8  
  43. Client characterset:    utf8  
  44. Conn.  characterset:    utf8  
  45. UNIX socket:            /tmp/mysql3306.sock  
  46. Uptime:                 30 min 18 sec  
  47.   
  48. Threads: 3  Questions: 64  Slow queries: 2  Opens: 48  Flush tables: 1  Open tables: 41  Queries per second avg: 0.035  
  49. --------------  
  50.   
  51. mysql> exit  
  52. Bye  
  53. [root@localhost data]# ll  
  54. total 8  
  55. drwxr-xr-x. 3 root  root  4096 Jan 22 20:40 logs  
  56. drwxr-xr-x. 3 mysql mysql 4096 Jan 22 17:31 mysql  
  57. [root@localhost data]# cd /tmp/  
  58. [root@localhost tmp]# ll  
  59. total 20  
  60. srwxrwxrwx. 1 mysql mysql    0 Jan 22 20:45 mysql3306.sock  
  61. drwx------. 2 root  root  4096 Jan 22 16:30 ssh-kLGZrU1461  
  62. drwxr-xr-x. 2 root  root  4096 Jan 23  2013 vmware-config0  
  63. drwxrwxrwt. 2 root  root  4096 Jan 23  2013 VMwareDnD  
  64. drwxr-xr-x. 2 root  root  4096 Jan 23  2013 vmware-fonts0  
  65. drwx------. 2 root  root  4096 Jan 22 16:28 vmware-root  
  66. -rw-------. 1 root  root     0 Jan 22  2013 yum.log  
  67. [root@localhost tmp]# rm -f mysql3306.sock   
  68. [root@localhost tmp]# ll  
  69. total 20  
  70. drwx------. 2 root root 4096 Jan 22 16:30 ssh-kLGZrU1461  
  71. drwxr-xr-x. 2 root root 4096 Jan 23  2013 vmware-config0  
  72. drwxrwxrwt. 2 root root 4096 Jan 23  2013 VMwareDnD  
  73. drwxr-xr-x. 2 root root 4096 Jan 23  2013 vmware-fonts0  
  74. drwx------. 2 root root 4096 Jan 22 16:28 vmware-root  
  75. -rw-------. 1 root root    0 Jan 22  2013 yum.log  
  76. [root@localhost tmp]# mysql -uroot -p -hlocalhost -P3306  
  77. Enter password:   
  78. ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql3306.sock' (2)  
  79. [root@localhost tmp]# mysql -uroot -p -h127.0.0.1 -P3306  
  80. Enter password:   
  81. ERROR 1130 (HY000): Host '127.0.0.1' is not allowed to connect to this MySQL server  
  82. [root@localhost tmp]# mysql -uroot -P -P3306              
  83. Unknown suffix '-' used for variable 'port' (value '-P3306')  
  84. mysql: Error while setting value '-P3306' to 'port'  
  85. [root@localhost tmp]# mysql -uroot -p -P3306  
  86. Enter password:   
  87. ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql3306.sock' (2)  
  88. [root@localhost tmp]# ll  
  89. total 20  
  90. drwx------. 2 root root 4096 Jan 22 16:30 ssh-kLGZrU1461  
  91. drwxr-xr-x. 2 root root 4096 Jan 23  2013 vmware-config0  
  92. drwxrwxrwt. 2 root root 4096 Jan 23  2013 VMwareDnD  
  93. drwxr-xr-x. 2 root root 4096 Jan 23  2013 vmware-fonts0  
  94. drwx------. 2 root root 4096 Jan 22 16:28 vmware-root  
  95. -rw-------. 1 root root    0 Jan 22  2013 yum.log  
  96. [root@localhost tmp]# mysql -uxm_dba -p -h192.168.161.82 -P3306  
  97. Enter password:   
  98. Welcome to the MySQL monitor.  Commands end with ; or \g.  
  99. Your MySQL connection id is 6  
  100. Server version: 5.5.18-log Source distribution  
  101.   
  102. Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.  
  103.   
  104. Oracle is a registered trademark of Oracle Corporation and/or its  
  105. affiliates. Other names may be trademarks of their respective  
  106. owners.  
  107.   
  108. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.  
  109.   
  110. mysql> use mysql;  
  111. Reading table information for completion of table and column names  
  112. You can turn off this feature to get a quicker startup with -A  
  113.   
  114. Database changed  
  115. mysql> select user,host,password from user;  
  116. +----------+-----------+-------------------------------------------+  
  117. | user     | host      | password                                  |  
  118. +----------+-----------+-------------------------------------------+  
  119. | root     | localhost | *548E9BC80642113DD04156ED194459C8EAC2A08E |  
  120. | xm_dba   | 192.168.% | *548E9BC80642113DD04156ED194459C8EAC2A08E |  
  121. | rep_user | 192.168.% | *548E9BC80642113DD04156ED194459C8EAC2A08E |  
  122. +----------+-----------+-------------------------------------------+  
  123. 3 rows in set (0.00 sec)  
  124.   
  125. mysql> FLUSH PRIVILEGES;  
  126. Query OK, 0 rows affected (0.00 sec)  
  127.   
  128. mysql> create table test.user(select * from mysql.user where user='root');  
  129. Query OK, 1 row affected (0.02 sec)  
  130. Records: 1  Duplicates: 0  Warnings: 0  
  131.   
  132. mysql> select * from test.user\G  
  133. *************************** 1. row ***************************  
  134.                   Host: localhost  
  135.                   User: root  
  136.               Password: *548E9BC80642113DD04156ED194459C8EAC2A08E  
  137.            Select_priv: Y  
  138.            Insert_priv: Y  
  139.            Update_priv: Y  
  140.            Delete_priv: Y  
  141.            Create_priv: Y  
  142.              Drop_priv: Y  
  143.            Reload_priv: Y  
  144.          Shutdown_priv: Y  
  145.           Process_priv: Y  
  146.              File_priv: Y  
  147.             Grant_priv: Y  
  148.        References_priv: Y  
  149.             Index_priv: Y  
  150.             Alter_priv: Y  
  151.           Show_db_priv: Y  
  152.             Super_priv: Y  
  153.  Create_tmp_table_priv: Y  
  154.       Lock_tables_priv: Y  
  155.           Execute_priv: Y  
  156.        Repl_slave_priv: Y  
  157.       Repl_client_priv: Y  
  158.       Create_view_priv: Y  
  159.         Show_view_priv: Y  
  160.    Create_routine_priv: Y  
  161.     Alter_routine_priv: Y  
  162.       Create_user_priv: Y  
  163.             Event_priv: Y  
  164.           Trigger_priv: Y  
  165. Create_tablespace_priv: Y  
  166.               ssl_type:   
  167.             ssl_cipher:   
  168.            x509_issuer:   
  169.           x509_subject:   
  170.          max_questions: 0  
  171.            max_updates: 0  
  172.        max_connections: 0  
  173.   max_user_connections: 0  
  174.                 plugin:   
  175.  authentication_string:   
  176. 1 row in set (0.00 sec)  
  177.   
  178. mysql> update test.user set host='127.0.0.1';  
  179. Query OK, 1 row affected (0.01 sec)  
  180. Rows matched: 1  Changed: 1  Warnings: 0  
  181.   
  182. mysql> insert into mysql.user select * from test.user;  
  183. Query OK, 1 row affected (0.00 sec)  
  184. Records: 1  Duplicates: 0  Warnings: 0  
  185.   
  186. mysql> select user,host,password from user;  
  187. +----------+-----------+-------------------------------------------+  
  188. | user     | host      | password                                  |  
  189. +----------+-----------+-------------------------------------------+  
  190. | root     | localhost | *548E9BC80642113DD04156ED194459C8EAC2A08E |  
  191. | root     | 127.0.0.1 | *548E9BC80642113DD04156ED194459C8EAC2A08E |  
  192. | xm_dba   | 192.168.% | *548E9BC80642113DD04156ED194459C8EAC2A08E |  
  193. | rep_user | 192.168.% | *548E9BC80642113DD04156ED194459C8EAC2A08E |  
  194. +----------+-----------+-------------------------------------------+  
  195. 4 rows in set (0.00 sec)  
  196.   
  197. mysql> FLUSH PRIVILEGES;  
  198. Query OK, 0 rows affected (0.00 sec)  
  199.   
  200. mysql> exit  
  201. Bye  
  202. [root@localhost tmp]# mysql -uroot -p -h127.0.0.1 -P3306             
  203. Enter password:   
  204. Welcome to the MySQL monitor.  Commands end with ; or \g.  
  205. Your MySQL connection id is 7  
  206. Server version: 5.5.18-log Source distribution  
  207.   
  208. Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.  
  209.   
  210. Oracle is a registered trademark of Oracle Corporation and/or its  
  211. affiliates. Other names may be trademarks of their respective  
  212. owners.  
  213.   
  214. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.  
  215.   
  216. mysql> \s  
  217. --------------  
  218. mysql  Ver 14.14 Distrib 5.5.18, for Linux (x86_64) using readline 5.1  
  219.   
  220. Connection id:          7  
  221. Current database:  
  222. Current user:           root@127.0.0.1  
  223. SSL:                    Not in use  
  224. Current pager:          stdout  
  225. Using outfile:          ''  
  226. Using delimiter:        ;  
  227. Server version:         5.5.18-log Source distribution  
  228. Protocol version:       10  
  229. Connection:             127.0.0.1 via TCP/IP  
  230. Server characterset:    utf8  
  231. Db     characterset:    utf8  
  232. Client characterset:    utf8  
  233. Conn.  characterset:    utf8  
  234. TCP port:               3306  
  235. Uptime:                 41 min 30 sec  
  236.   
  237. Threads: 3  Questions: 106  Slow queries: 8  Opens: 50  Flush tables: 1  Open tables: 42  Queries per second avg: 0.042  
  238. --------------  
  239.   
  240. mysql> show grants for 'root'@'127.0.0.1';  
  241. +----------------------------------------------------------------------------------------------------------------------------------------+  
  242. | Grants for root@127.0.0.1                                                                                                              |  
  243. +----------------------------------------------------------------------------------------------------------------------------------------+  
  244. | GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' IDENTIFIED BY PASSWORD '*548E9BC80642113DD04156ED194459C8EAC2A08E' WITH GRANT OPTION |  
  245. +----------------------------------------------------------------------------------------------------------------------------------------+  
  246. 1 row in set (0.00 sec)  
  247. >mysql> show grants for 'xm_dba'@'192.168.%';  
  248. +------------------------------------------------------------------------------------------------------------------------+  
  249. | GRANT ALL PRIVILEGES ON *.* TO 'xm_dba'@'192.168.%' IDENTIFIED BY PASSWORD '*548E9BC80642113DD04156ED194459C8EAC2A08E' |  
  250. +------------------------------------------------------------------------------------------------------------------------+  
  251. 1 row in set (0.00 sec)</span>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值