Home Database Mysql Tutorial 连接MySQL数据库失败的原因

连接MySQL数据库失败的原因

Jun 07, 2016 pm 03:15 PM
linux mysql reason database Enter connect

欢迎进入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的时候自动使用管道连接本机,不会出现连接问题。

连接MySQL数据库失败的原因

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

Docker on Linux: Containerization for Linux Systems Docker on Linux: Containerization for Linux Systems Apr 22, 2025 am 12:03 AM

Docker is important on Linux because Linux is its native platform that provides rich tools and community support. 1. Install Docker: Use sudoapt-getupdate and sudoapt-getinstalldocker-cedocker-ce-clicotainerd.io. 2. Create and manage containers: Use dockerrun commands, such as dockerrun-d--namemynginx-p80:80nginx. 3. Write Dockerfile: Optimize the image size and use multi-stage construction. 4. Optimization and debugging: Use dockerlogs and dockerex

How to safely store JavaScript objects containing functions and regular expressions to a database and restore? How to safely store JavaScript objects containing functions and regular expressions to a database and restore? Apr 19, 2025 pm 11:09 PM

Safely handle functions and regular expressions in JSON In front-end development, JavaScript is often required...

Explain the purpose of foreign keys in MySQL. Explain the purpose of foreign keys in MySQL. Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

SQL vs. MySQL: Clarifying the Relationship Between the Two SQL vs. MySQL: Clarifying the Relationship Between the Two Apr 24, 2025 am 12:02 AM

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

CentOS: Security, Stability, and Performance CentOS: Security, Stability, and Performance Apr 21, 2025 am 12:11 AM

CentOS is the first choice for server and enterprise environments for its superior security, stability and performance. 1) Security provides forced access control through SELinux to improve system security. 2) Stability is supported by the LTS version for up to 10 years to ensure the stability of the system. 3) Performance significantly improves system response speed and resource utilization by optimizing kernel and system configuration.

See all articles