mysql问题(musees经典) - 问题 the to socket that MySQL you can is You server

来源:百度文库 编辑:神马文学网 时间:2024/04/17 05:21:42
Mysql连接错误:
mysql_real_connetc() failed:
Error 2002 (Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘ (2))。
这是因为Mysql有两种方式连接:
1、Unix socket方式,-p用localhost关键字,也就是本地连接,速度快
2、TCP/IP方式, -p指定IP地址。
其中第一种方式需要一个socket文件,文件类型域是“s”,后缀为.sock的连接文件。由于socket文件是在Mysql的数据存放目录下面生成的,当我们改变Mysql的数据存放地址时,也需要改变socket的存放地址,反应在/etc/my.cnf中:
[client]
port=3306
socket   =      /home/mysql_data/mysql/mysql.sock
[mysqld]
datadir=/home/mysql_data/mysql
socket=/home/mysql_data/mysql/mysql.sock
[mysql.server]
user=mysql
basedir=/home/mysql_data
[safe_mysqld]
err-log=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
这样服务器和客户端都需要指定socket文件,才能在用localhost访问数据库时正确的找到socket文件。
但现在的问题是
在命令行下面可以正常操作;
在C代码里面不能,所以只好改用IP地址操作。
.2.3 Can‘t connect to [local] MySQL server Error
....
Here are some reasons the Can‘t connect to local MySQL server error might occur:
1: mysqld is not running.
2:You are running on a system that uses MIT-pthreads. If you are runningon a system that doesn‘t have native threads, mysqld uses theMIT-pthreads package. See section 2.2.2 Operating Systems Supported byMySQL. However, not all MIT-pthreads versions support Unix sockets. Ona system without sockets support you must always specify the hostnameexplicitly when connecting to the server. Try using this command tocheck the connection to the server:
shell>; mysqladmin -h `hostname` version
3:Someone has removed the Unix socket that mysqld uses (default`/tmp/mysqld.sock‘). You might have a cron job that removes the MySQLsocket (for example, a job that removes old files from the `/tmp‘directory). You can always run mysqladmin version and check that thesocket mysqladmin is trying to use really exists. The fix in this caseis to change the cron job to not remove `mysqld.sock‘ or to place thesocket somewhere else. See section A.4.5 How to Protect or Change theMySQL Socket File `/tmp/mysql.sock‘.
4: You have started themysqld server with the --socket=/path/to/socket option. If you changethe socket pathname for the server, you must also notify the MySQLclients about the new path. You can do this by providing the socketpath as an argument to the client. See section A.4.5 How to Protect orChange the MySQL Socket File `/tmp/mysql.sock‘.
5: You areusing Linux and one thread has died (core dumped). In this case youmust kill the other mysqld threads (for example, with the mysql_zapscript before you can start a new MySQL server. See section A.4.1 WhatTo Do If MySQL Keeps Crashing.
6: You may not have read andwrite privilege to either the directory that holds the socket file orprivilege to the socket file itself. In this case you have to eitherchange the privilege for the directory / file or restart mysqld so thatit uses a directory that you can access.
If you get the errormessage Can‘t connect to MySQL server on some_hostname, you can try thefollowing things to find out what the problem is :
Check ifthe server is up by doing telnet your-host-name tcp-ip-port-number andpress Enter a couple of times. If there is a MySQL server running onthis port you should get a responses that includes the version numberof the running MySQL server. If you get an error like telnet: Unable toconnect to remote host: Connection refused, then there is no serverrunning on the given port.
Try connecting to the mysqld daemonon the local machine and check the TCP/IP port that mysqld it‘sconfigured to use (variable port) with mysqladmin variables.
Check that your mysqld server is not started with the --skip-networking option.
建议把mysql.sock保护一下,不然给个调皮的luser删了就麻烦了。
A.4.5 How to Protect or Change the MySQL Socket File `/tmp/mysql.sock‘
Ifyou have problems with the fact that anyone can delete the MySQLcommunication socket `/tmp/mysql.sock‘, you can, on most versions ofUnix, protect your `/tmp‘ filesystem by setting the sticky bit on it.Log in as root and do the following:
shell>; chmod +t /tmp
This will protect your `/tmp‘ filesystem so that files can be deleted only by their owners or the superuser (root).
You can check if the sticky bit is set by executing ls -ld /tmp. If the last permission bit is t, the bit is set.
You can change the place where MySQL uses / puts the socket file the following ways:
Specify the path in a global or local option file. For example, put in /etc/my.cnf:
[client]
socket=path-for-socket-file
[mysqld]
socket=path-for-socket-file
See section 4.1.2 `my.cnf‘ Option Files.
Specifying this on the command-line to safe_mysqld and most clients with the --socket=path-for-socket-file option.
Specify the path to the socket in the MYSQL_UNIX_PORT environment variable.
Definingthe path with the configure option--with-unix-socket-path=path-for-socket-file. See section 2.3.3 Typicalconfigure Options.
You can test that the socket works with this command:
shell>; mysqladmin --socket=/path/to/socket version