首页>>帮助中心>>美国VPS解决因为mysql.sock文件而无法连接数据库的问题

美国VPS解决因为mysql.sock文件而无法连接数据库的问题

2024/11/28 36次

美国VPS解决因为mysql.sock文件而无法连接数据库的问题


网页不能正常显示并报错

Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock'(2)

同时还伴有CPU占用异常,疯狂给我发来邮件。连上服务器,看了下日志,大概是这样的:

2018-02-06 00:49:26 32762 [Note] Plugin ‘FEDERATED’ is disabled.
2018-02-06 00:49:26 32762 [Note] InnoDB: Using atomics to ref count buffer pool pages
2018-02-06 00:49:26 32762 [Note] InnoDB: The InnoDB memory heap is disabled
2018-02-06 00:49:26 32762 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2018-02-06 00:49:26 32762 [Note] InnoDB: Memory barrier is not used
2018-02-06 00:49:26 32762 [Note] InnoDB: Compressed tables use zlib 1.2.3
2018-02-06 00:49:26 32762 [Note] InnoDB: Using Linux native AIO
2018-02-06 00:49:26 32762 [Note] InnoDB: Using CPU crc32 instructions
2018-02-06 00:49:26 32762 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2018-02-06 00:49:26 32762 [Note] InnoDB: Completed initialization of buffer pool
2018-02-06 00:49:26 32762 [Note] InnoDB: Highest supported file format is Barracuda.
2018-02-06 00:49:26 32762 [Note] InnoDB: The log sequence numbers 8671802 and 8671802 in ibdata files do not match the log sequence number 9616217 in the ib_logfiles!
2018-02-06 00:49:26 32762 [Note] InnoDB: Database was not shutdown normally!
2018-02-06 00:49:26 32762 [Note] InnoDB: Starting crash recovery.
2018-02-06 00:49:26 32762 [Note] InnoDB: Reading tablespace information from the .ibd files…
2018-02-06 00:49:26 32762 [Note] InnoDB: Restoring possible half-written data pages
2018-02-06 00:49:26 32762 [Note] InnoDB: from the doublewrite buffer…
2018-02-06 00:49:32 32762 [Note] InnoDB: 128 rollback segment(s) are active.
180206 00:49:32 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
180206 00:59:26 mysqld_safe Logging to ‘/var/log/mysqld.log’.
180206 00:59:26 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

好的,阅读之后发现:

[Note] InnoDB: Initializing buffer pool, size = 128.0M

[Note] InnoDB: Completed initialization of buffer pool

原因大概是,内存不够造成的mysql异常,而mysql.sock是一个临时文件,在mysql启动时会自动生成,我的服务器未启动,自然就没有mysql.sock文件。同时,看了下mysql服务的状态,并不是running

想到了以下解决方式

增加内存
增加swap
减少InnoDB buffer pool的内存占用。
最终我采取了第二种方式,下面是操作步骤:

创建一个大小为256M的文件
dd if=/dev/zero of=/swapfile bs=1024 count=262144
把这个文件变成swap文件
mkswap /swapfile
启用这个swap文件
swapon /swapfile
找到并编辑/etc/fstab文件,使在每次开机时自动加载swap文件:
/swapfile swap swap default 0 0
然后就好了,弄完之后,现在内存多了一个这个:

‘ Swap: 255M 36K 255M’

当然之前也看到了什么创建软连接

ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock

购买使用一诺网络美国服务器,可以极大降低初创企业、中小企业以及个人开发者等用户群体的整体IT使用成本,无需亲自搭建基础设施、简化了运维和管理的日常工作量,使用户能够更专注于自身的业务发展和创新。美国服务器低至49元/月,购买链接:https://www.enuoidc.com/vpszq.html?typeid=3