Mysql连接错误:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (

1.首先查看  /tmp 目录下有没有 mysql.sock

             在终端上;prompt> ls /tmp | grep mysql.sock

    结果发现该目录下没有mysql.sock

 

2,创建 /tmp/mysql.sock 的方法

         首先sudo  vi /etc/mysql/my.cnf ,通过底行的查找命令 : :/socket/   就找到 了socket的路径;

 

# The MySQL database server configuration file. # # You can copy this to one of: # - "/etc/mysql/my.cnf" to set global options, # - "~/.my.cnf" to set user-specific options. # # One can use all long options that the program supports. # Run program with --help to get a list of available options and with # --print-defaults to see which it would actually understand and use. # # For explanations see # http://dev.mysql.com/doc/mysql/en/server-system-variables.html # This will be passed to all mysql clients # It has been reported that passwords should be enclosed with ticks/quotes # escpecially if they contain "#" chars... # Remember to edit /etc/mysql/debian.cnf when changing the socket location. [client] port = 3306 socket = /var/run/mysqld/mysqld.sock # Here is entries for some specific programs # The following values assume you have at least 32M ram # This was formally known as [safe_mysqld]. Both versions are currently parsed. [mysqld_safe] socket = /var/run/mysqld/mysqld.sock nice = 0 [mysqld] # # * Basic Settings # # # * IMPORTANT # If you make changes to these settings and your system uses apparmor, you may # also need to also adjust /etc/apparmor.d/usr.sbin.mysqld. # :/socket/ 

# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

# This will be passed to all mysql clients
# It has been reported that passwords should be enclosed with ticks/quotes
# escpecially if they contain "#" chars...
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.
[client]
port            = 3306
socket          = /var/run/mysqld/mysqld.sock

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

# This was formally known as [safe_mysqld]. Both versions are currently parsed.
[mysqld_safe]
socket          = /var/run/mysqld/mysqld.sock
nice            = 0

[mysqld]
#
# * Basic Settings
#

#
# * IMPORTANT
#   If you make changes to these settings and your system uses apparmor, you may
#   also need to also adjust /etc/apparmor.d/usr.sbin.mysqld.
#

:/socket/

 

 

 

socket          = /var/run/mysqld/mysqld.sock

 

然后创建 链接文件:sudo ln -s /var/run/mysqld.sock  /tmp/mysql.sock

 

3.重新测试一下问题解决;

 

说明: mysql可以通过 tcp/ip 连接,也可以通过unix socket 链接,好像后者更快;以下时 c api 下实现的链接;

 

if (!mysql_real_connect(&mysql, NULL, user, password, database, 0, NULL, 0)) { fprintf(stderr, "connect error: %d: %s/n", mysql_errno(&mysql), mysql_error(&mysql)); exit(1); } */ if(!mysql_real_connect(&mysql, server, user, password, database, port, socket, flag)) { fprintf(stderr, "connect error: errono: %d ; error infor: %s/n", mysql_errno(&mysql), mysql_error(&mysql) ); exit(1); }

当连接参数中 host 指派为NULL  或者为 “localhost” ,且端口号指派为0, socket 指派为 NULL  时,默认为socket连接。 

 

你可能感兴趣的:(db,Linux)