连接MySQL数据库失败的原因
欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 连接mySQL数据库失败频繁,主要是什么原因造成的? 一年前,我开发了一个网站,租用的是linux下PHP+mySQL的虚拟空间,之前一直工作正常。 近半年来,却频繁出现连接数据库失败错误(一周1~2次),需
欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入
连接mySQL数据库失败频繁,主要是什么原因造成的? 一年前,我开发了一个网站,租用的是linux下PHP+mySQL的虚拟空间,之前一直工作正常。
近半年来,却频繁出现连接数据库失败错误(一周1~2次),需要找空间商重启mysql服务才行:
Warning: mysql_connect(): Can 't connect to MySQL server on 'xx.xx.xx.xx ' (4) in /www/users/1b2g.com/xxxx.php on line xxxx
找空间商投诉,空间商说是我自己的问题,说同主机的其他网站没有类似问题。
在网上搜索,似乎有些网友说,操作数据库不能用mysql_connect()和mysql_close()开关。照文中指示,将mysql_connect()改成了mysql_pconnect(),似乎正常了半个月时间。但是这半个月恰好因为百度调整,我的网站流量下降了。这2天,流量恢复,问题再度出现,而且不再是1周1~2,是天天出现:
Warning: mysql_pconnect(): Can 't connect to MySQL server on 'xx.xx.xx.xx ' (4) in /www/users/1b2g.com/xxxx.php on line xxxx
特别是今天,重启了也没用,又坏了,又联系不到客服,急死了。
请大家帮忙找找,到底是啥原因?是否和流量大小相关?应该怎么解决,谢谢!
回答:对于这个疑问,当时有人提出建议:优化一下程序,每页不要有过多的查询。 特别是循环体里面,尽量不要再有查询,否则很容易堵塞。应该考虑使用连接池
但是提问者又出现了问题:我已经尽可能减少每个页面的数据库连接数了。
曾经最开始的时候有个页面的连接数是比较多(因为开始为了代码复用,用了iframe嵌套),后来经优化最多的1个页面已经减到4个;其他一般1~2个。
昨天我已经把所有页面的连接数都降到2个以下,用户访问最多的页面(占访问总量的80%)只有1个连接。
但是今天中午问题又出现了。
从现象来看, 'xx.xx.xx.xx '应该你是用了域名或者IP作为host名,这样一旦DNS或者网关出问题,就会连不上本机端口,建议你使用localhost作为host名。localhost还会在不支持socket的时候自动使用管道连接本机,不会出现连接问题。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

VS Code To switch Chinese mode: Open the settings interface (Windows/Linux: Ctrl, macOS: Cmd,) Search for "Editor: Language" settings Select "Chinese" in the drop-down menu Save settings and restart VS Code

VS Code supports Chinese settings, which can be completed by following the steps: Open the settings panel and search for "locale". Set "locale.language" to "zh-CN" (Simplified Chinese) or "zh-TW" (Traditional Chinese). Save settings and restart VS Code. The settings menu, toolbar, code prompts, and documents will be displayed in Chinese. Other language settings can also be customized, such as file tag format, entry description, and diagnostic process language.

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

VS Code One-step/Next step shortcut key usage: One-step (backward): Windows/Linux: Ctrl ←; macOS: Cmd ←Next step (forward): Windows/Linux: Ctrl →; macOS: Cmd →

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version

Executing code in VS Code only takes six steps: 1. Open the project; 2. Create and write the code file; 3. Open the terminal; 4. Navigate to the project directory; 5. Execute the code with the appropriate commands; 6. View the output.

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.
