Chinaunix

标题: 【已解决】Can't connect to local MySQL server through socket '3306' [打印本页]

作者: bill01    时间: 2010-07-15 10:48
标题: 【已解决】Can't connect to local MySQL server through socket '3306'
本帖最后由 bill01 于 2010-07-15 13:53 编辑

连接mysql的时候出现问题:


ERROR 2002 (HY000): Can't connect to local MySQL server through socket '3306' (2)

具体
  1. [root@host]# mysql -u root -p -h localhost
  2. Enter password:
  3. ERROR 2002 (HY000): Can't connect to local MySQL server through socket '3306' (2)
  4. [root@host]# mysql -u root -p -h 127.0.0.1
  5. Enter password:
  6. Welcome to the MySQL monitor.  Commands end with ; or \g.
  7. Your MySQL connection id is 691
  8. Server version: 5.0.77 Source distribution

  9. Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
复制代码
很是不解
网站程序还是正常的
网站程序的mysql连接使用的是localhost
现在正常
可能的原因是昨天我修改了下my.conf 修改后就出错了
连不上了 mysqldump连接也是这样。
但问题是我配置文件备份过的 用备份的配置文件恢复也是不行的
求解答
作者: bill01    时间: 2010-07-15 10:49
上网查了好久 说的办法都搞不定
求高手帮忙解决下
作者: surpass_li    时间: 2010-07-15 10:55
不指定主机试一下
[root@host]# mysql -u root -p
作者: bill01    时间: 2010-07-15 11:04
不指定 主机是这样的
  1. mysql -u root -p
  2. Enter password:
  3. ERROR 2002 (HY000): Can't connect to local MySQL server through socket '3306' (2)
复制代码
另外 查看端口是
  1. netstat -nat | grep 3306
  2. tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN
复制代码

作者: bill01    时间: 2010-07-15 11:07
把 /usr/share/doc/mysql-server-5.0.77/examples/my-large.cnf
覆盖 /etc/my.cnf
把 mysql进程kill掉 再启动
涛声依旧
NND
作者: bill01    时间: 2010-07-15 11:12
  1. mysql -u root -p -S /var/lib/mysql/mysql.sock
  2. Enter password:
  3. Welcome to the MySQL monitor.  Commands end with ; or \g.
  4. Your MySQL connection id is 261
  5. Server version: 5.0.77-log Source distribution

  6. Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
复制代码
指定了 mysql.sock的位置这样是OK的
mysql.sock的位置  /var/lib/mysql/mysql.sock  
但是 /var/lib/mysql的所有者是mysql
  1. ls -l /var/lib |grep mysql
  2. drwxr-xr-x 31 mysql  mysql   4096 Jul 15 11:01 mysql
复制代码

作者: shang2010    时间: 2010-07-15 11:12
问题不太懂,
ping 127.0.0.1
再看看防火墙有没有把自己给和谐掉
作者: shang2010    时间: 2010-07-15 11:14
了解了


[root@host]# mysql -u root -p -h host
作者: surpass_li    时间: 2010-07-15 11:15
不用127.0.0.1 用你本机的真实IP试一下呢
作者: bill01    时间: 2010-07-15 11:15
问题不太懂,
ping 127.0.0.1
再看看防火墙有没有把自己给和谐掉
shang2010 发表于 2010-07-15 11:12



    和防火墙应该没关系
因为是本地连接
另外ping 127.0.0.1正常
ping localhost也正常
问题应该不在这里
但具体哪里 找不到 头大
作者: shang2010    时间: 2010-07-15 11:18
[root@host]# mysql -u root -p -h 127.0.0.1
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 691
Server version: 5.0.77 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.


--
这个日志不是正常的么
作者: shang2010    时间: 2010-07-15 11:19
我看你修改了主机名成host了,,
我头也被大了
作者: bill01    时间: 2010-07-15 11:32
我看你修改了主机名成host了,,
我头也被大了
shang2010 发表于 2010-07-15 11:19



    不是 那个 主机名 不是这个 是我复制到cu的时候改的  

作者: xiao7ng    时间: 2010-07-15 11:32
看下hosts文件吧
作者: bill01    时间: 2010-07-15 11:32
回复 11# shang2010


    我想知道的是 为啥 直接 mysql -u root -p
或者mysql -u root -p -h localhost
不行呢
作者: ubuntuer    时间: 2010-07-15 11:33
mysql对于localhost 和 127.0.0.1的处理方式是不一样的!
使用localhost的话 会采用unix socket去连接 而不是我们所期望的tcp/ip

unix socket是同一台机器上不同进程间的通信机制
而ip socket是网络上不同主机之间进程的通讯机制
作者: bill01    时间: 2010-07-15 11:34
回复 14# xiao7ng
  1. less /etc/hosts
  2. # Do not remove the following line, or various programs
  3. # that require network functionality will fail.
  4. 127.0.0.1              host180 localhost.localdomain localhost
  5. ::1             localhost6.localdomain6 localhost6
复制代码

作者: bill01    时间: 2010-07-15 11:36
mysql对于localhost 和 127.0.0.1的处理方式是不一样的!
使用localhost的话 会采用unix socket去连接 而不 ...
ubuntuer 发表于 2010-07-15 11:33



    那请问这位该咋样才能两种方式都可以连接呢
另外不解的是
在PHP程序中用localhost是正常的
作者: ruochen    时间: 2010-07-15 12:29
配置文件中有socket指定路径么?
作者: bill01    时间: 2010-07-15 12:38
  1. # Example MySQL config file for large systems.
  2. #
  3. # This is for a large system with memory = 512M where the system runs mainly
  4. # MySQL.
  5. #
  6. # You can copy this file to
  7. # /etc/my.cnf to set global options,
  8. # mysql-data-dir/my.cnf to set server-specific options (in this
  9. # installation this directory is /var/lib/mysql) or
  10. # ~/.my.cnf to set user-specific options.
  11. #
  12. # In this file, you can use all long options that a program supports.
  13. # If you want to know which options a program supports, run the program
  14. # with the "--help" option.

  15. # The following options will be passed to all MySQL clients
  16. [client]
  17. #password       = your_password
  18. port            = 3306
  19. socket          = /var/lib/mysql/mysql.sock

  20. # Here follows entries for some specific programs

  21. # The MySQL server
  22. [mysqld]
  23. port            = 3306
  24. socket          = /var/lib/mysql/mysql.sock
  25. skip-locking
  26. key_buffer = 256M
  27. max_allowed_packet = 1M
  28. table_cache = 256
  29. sort_buffer_size = 1M
  30. read_buffer_size = 1M
  31. read_rnd_buffer_size = 4M
  32. myisam_sort_buffer_size = 64M
  33. thread_cache_size = 8
  34. query_cache_size= 16M
  35. # Try number of CPU's*2 for thread_concurrency
  36. thread_concurrency = 8

  37. # Don't listen on a TCP/IP port at all. This can be a security enhancement,
  38. # if all processes that need to connect to mysqld run on the same host.
  39. # All interaction with mysqld must be made via Unix sockets or named pipes.
  40. # Note that using this option without enabling named pipes on Windows
  41. # (via the "enable-named-pipe" option) will render mysqld useless!
  42. #
  43. #skip-networking

  44. # Disable Federated by default
  45. skip-federated

  46. # Replication Master Server (default)
  47. # binary logging is required for replication
  48. log-bin=mysql-bin

  49. # required unique id between 1 and 2^32 - 1
  50. # defaults to 1 if master-host is not set
  51. # but will not function as a master if omitted
  52. server-id       = 1

  53. # Replication Slave (comment out master section to use this)
  54. #
  55. # To configure this host as a replication slave, you can choose between
  56. # two methods :
  57. #
  58. # 1) Use the CHANGE MASTER TO command (fully described in our manual) -
  59. #    the syntax is:
  60. #
  61. #    CHANGE MASTER TO MASTER_HOST=<host>, MASTER_PORT=<port>,
  62. #    MASTER_USER=<user>, MASTER_PASSWORD=<password> ;
  63. #
  64. #    where you replace <host>, <user>, <password> by quoted strings and
  65. #    <port> by the master's port number (3306 by default).
  66. #
  67. #    Example:
  68. #
  69. #    CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,
  70. #    MASTER_USER='joe', MASTER_PASSWORD='secret';
  71. #
  72. # OR
  73. #
  74. # 2) Set the variables below. However, in case you choose this method, then
  75. #    start replication for the first time (even unsuccessfully, for example
  76. #    if you mistyped the password in master-password and the slave fails to
  77. #    connect), the slave will create a master.info file, and any later
  78. #    change in this file to the variables' values below will be ignored and
  79. #    overridden by the content of the master.info file, unless you shutdown
  80. #    the slave server, delete master.info and restart the slaver server.
  81. #    For that reason, you may want to leave the lines below untouched
  82. #    (commented) and instead use CHANGE MASTER TO (see above)
  83. #
  84. # required unique id between 2 and 2^32 - 1
  85. # (and different from the master)
  86. # defaults to 2 if master-host is set
  87. # but will not function as a slave if omitted
  88. #server-id       = 2
  89. #
  90. # The replication master for this slave - required
  91. #master-host     =   <hostname>
  92. #
  93. # The username the slave will use for authentication when connecting
  94. # to the master - required
  95. #master-user     =   <username>
  96. #
  97. # The password the slave will authenticate with when connecting to
  98. # the master - required
  99. #master-password =   <password>
  100. #
  101. # The port the master is listening on.
  102. # optional - defaults to 3306
  103. #master-port     =  <port>
  104. #
  105. # binary logging - not required for slaves, but recommended
  106. #log-bin=mysql-bin

  107. # Point the following paths to different dedicated disks
  108. #tmpdir         = /tmp/         
  109. #log-update     = /path-to-dedicated-directory/hostname

  110. # Uncomment the following if you are using BDB tables
  111. #bdb_cache_size = 64M
  112. #bdb_max_lock = 100000

  113. # Uncomment the following if you are using InnoDB tables
  114. #innodb_data_home_dir = /var/lib/mysql/
  115. #innodb_data_file_path = ibdata1:10M:autoextend
  116. #innodb_log_group_home_dir = /var/lib/mysql/
  117. #innodb_log_arch_dir = /var/lib/mysql/
  118. # You can set .._buffer_pool_size up to 50 - 80 %
  119. # of RAM but beware of setting memory usage too high
  120. #innodb_buffer_pool_size = 256M
  121. #innodb_additional_mem_pool_size = 20M
  122. # Set .._log_file_size to 25 % of buffer pool size
  123. #innodb_log_file_size = 64M
  124. #innodb_log_buffer_size = 8M
  125. #innodb_flush_log_at_trx_commit = 1
  126. #innodb_lock_wait_timeout = 50

  127. [mysqldump]
  128. quick
  129. max_allowed_packet = 16M

  130. [mysql]
  131. no-auto-rehash
  132. # Remove the next comment character if you are not familiar with SQL
  133. #safe-updates

  134. [isamchk]
  135. key_buffer = 128M
  136. sort_buffer_size = 128M
  137. read_buffer = 2M
  138. write_buffer = 2M

  139. [myisamchk]
  140. key_buffer = 128M
  141. sort_buffer_size = 128M
  142. read_buffer = 2M
  143. write_buffer = 2M

  144. [mysqlhotcopy]
  145. interactive-timeout
复制代码
这是配置文件
作者: bill01    时间: 2010-07-15 13:52

中午吃了饭 一边查资料 一边 修改 但忘记修改到哪一步 竟然OK了
NND 算了 搞定就行
晚点慢慢研究是哪里的原因

谢谢 上面各位大哥 回帖




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2