Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'的解决

上午用mysql还顺顺利利的,中途因为java向mysql表插入中文数据导致乱码问题,就sudo vi /etc/mysql/my.cnf配置文件中加入编码修改的语句,还是没有解决编码问题。。。。后来...后来下午继续倒腾编码问题,启动mysql服务【mysql -u root -p】,就悲了个剧了:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock

疯狂网上搜解决办法。。。女汉子形象再次重现:刘海一撇,袖子一撸,美好淑女形象全无。。。。。。。。。。。。

                                    ---------------------------------------------------------------------------------------

查的解决方案有:

(1)方案1.
   #ps -A|grep mysql【由于启动不了mysql服务,执行该命令没有反应。如果启动了服务再执行该命令,输出进程号,如下】
   显示类似:
  1829 ?        00:00:00 mysqld_safe
   1876 ?        00:00:31 mysqld
  #kill -9 1829
  #kill -9 1876
  #/etc/init.d/mysql restart
  #mysql -u root -p
 
   他的麻烦解决了,我的还没。。。

(2)/etc/rc.d/init.d/mysqld status 看看mysql是否已经启动.【我输入该命令后,bash: /etc/rc.d/init.d/mysqld: 没有那个文件或目录】

 无果。。放弃。。。

(3)解决不能通过mysql .sock连接MySQL问题 这个问题主要提示是,不能通过 '/tmp/mysql .sock'连到服务器,而php标准配置正是用过'/tmp/mysql .sock',但是一些mysql 安装将 mysql .sock放在/var/lib/mysql .sock或者其他的什么地方,你可以通过修改/etc/my.cnf文件来修正它,打开文件,可以看到如下的东东:

   [mysql d]
  socket=/var/lib/mysql .sock
  改一下就好了,但也会引起其他的问题,如mysql 程序连不上了,再加一点:
  [mysql ]
  socket=/tmp/mysql .sock
  或者还可以通过修改php.ini中的配置来使php用其他的mysql .sock来连,这个大家自己去找找
  
  或者用这样的方法:
  ln -s /var/lib/mysql /mysql .sock /tmp/mysql .sock

  他的麻烦解决了,我的还么有。。。

(4)因为是没有启动mysql服务,所以不会生成mysql.sock文件

         修改权限:
         sudo chown -R mysql:mysql /var/lib/mysql

       再次启动mysql服务【mysql -u root -p】,乖乖,终于好了诶-&-还没搞清楚为啥是权限的问题。。。



                                                     -------------------------------------------------------------------------------

关于mysql的配置文件my.cnf参数的一些了解【转自http://blog.chinaunix.net/uid-26263325-id-3013041.html】

#
# 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]    $$$:以下选项会被MySQL客户端应用读取.
port        = 3306
socket    = /var/run/mysqld/mysqld.sock    $$$:用于本地连接的Unix套接字文件

# 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    $$$:用于本地连接的Unix套接字文件
nice        = 0    $$$:使用nice程序根据给定值来设置服务器的调度优先级

[mysqld]    $$$:MySQL 办事端
#
# * 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.
#

user        = mysql
socket        = /var/run/mysqld/mysqld.sock    $$$:为MySQL客户程序与服务器之间的本地通信指定一个套接字文件
port        = 3306    $$$:指定MsSQL侦听的端口
basedir        = /usr    $$$:使用该目录作为根目录(安装目录)。
datadir        = /var/lib/mysql   $$$:从该目录读取数据库文件
tmpdir        = /tmp
skip-external-locking
skip-name-resolve    $$$:禁止MySQL对外部连接进行DNS解析,使用这一选项可以消除MySQL进行DNS解析的时间。但需要注意,如果开启该选项,则所有远程主机连接授权都要使用IP地址方式,否则MySQL将无法正常处理连接请求!
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address        = 127.0.0.1
#
# * Fine Tuning
#
key_buffer        = 16M    $$$:key_buffer是用于索引块的缓冲区大小,增加它可得到更好处理的索引(对所有读和多重写)。对于内存在4GB左右的服务器该参数可设置为256M或384M。注意:该参数值设置的过大反而会是服务器整体效率降低!
max_allowed_packet    = 16M    $$$:包或任何生成的/中间字符串的最大大小。
thread_stack        = 192K
thread_cache_size       = 8    $$$:缓存可重用的线程数
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover         = BACKUP    $$$:存储引擎MyISAM设置为恢复模式。该选项值是DEFAULT、BACKUP、FORCE或QUICK值的任何组合.
#max_connections        = 100    $$$:MySQL服务器同时处理的数据库连接的最大数量(默认设置是100)。超过限制后会报 Too many connections 错误
#table_cache            = 64    $$$:为所有线程打开表的数量。增加该值能增加mysqld要求的文件描述符的数量。可以避免频繁的打开数据表产生的开销
#thread_concurrency     = 10    $$$:最大并发线程数,取值为服务器逻辑CPU数量×2,如果CPU支持H.T超线程,再×2
#
# * Query Cache Configuration
#
query_cache_limit    = 10M    $$$:只有小于此设定值的结果才会被缓冲,此设置用来保护查询缓冲,防止一个极大的结果集将其他所有的查询结果都覆盖.
query_cache_size        = 160M    $$$:指定MySQL查询结果缓冲区的大小
open_files_limit    = 2048    $$$:用来更改mysqld文件描述符的数量
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
#general_log_file        = /var/log/mysql/mysql.log
#general_log             = 1

log_error                = /var/log/mysql/error.log    $$$:将错误日志写入给定的文件


# Here you can see queries with especially long duration
#log_slow_queries    = /var/log/mysql/mysql-slow.log
#long_query_time = 2
#log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
#       other settings you may need to change.
#server-id        = 1
#log_bin            = /var/log/mysql/mysql-bin.log
expire_logs_days    = 10    $$$:二进制日志自动删除的天数。默认值为0,表示“没有自动删除”。启动时和二进制日志循环时可能删除。
max_binlog_size         = 100M    $$$:如果二进制日志写入的内容超出给定值,日志就会发生滚动。你不能将该变量设置为大于1GB或小于4096字节。 默认值是1GB。
#binlog_do_db        = include_database_name
#binlog_ignore_db    = include_database_name
#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem



[mysqldump]    $$$:转存储数据库的实用程序

quick
quote-names
max_allowed_packet    = 16M    $$$:服务器和客户端之间最大能发送的可能信息包

[mysql]
#no-auto-rehash    # faster start of mysql but no tab completition

[isamchk]    $$$:完成表的分析和优化,以及在表损坏时进行崩溃恢复的程序。
key_buffer        = 16M

#
# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/

你可能感兴趣的:(Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'的解决)