MySQL连接hang住问题分析
Linux c多线程连接mysql数据库,每次都是短连接,操作完后就释放连接,有时候会出现mysql_real_connect挂住的现象,挂住超时mysql
【问题现象】:
1. Linux c多线程连接mysql数据库,每次都是短连接,操作完后就释放连接,有时候会出现mysql_real_connect挂住的现象
2. 挂住超时mysql_real_connect返回后报错如下:Lostconnection to MySQL server at 'reading initial communication packet', systemerror: 104,返错后线程号没变,会继续往下运行
【初步原因分析】:
1. mysql_real_connect连接数据库, 没有显式调用超时时间,重连什么的,,使用的默认值:
2. 昨天拿c mysqlclient的源码进行了分析测试,发现以下几个配置项的默认值(如果调mysql_real_connect之前没有设置任何属性,mysql client端的机器上/etc/my.cnf也没有配置)如下:
connect_timeout = 0
read_timeout = 0
write_timeout = 0
reconnect = 0 //1表示自动重连
所以这些参数是需要显式设置的。
{net = {vio = 0x7b8f150, options = {connect_timeout = 0, read_timeout = 0, write_timeout = 0,}
3. Mysqlclient中报错的代码如下(与服务端连接建立后,读解析包的时候失败了):
/*
Part 1: Connection established, read and parse first packet
*/
if ((pkt_length=cli_safe_read(mysql)) == packet_error)
{
if (mysql->net.last_errno == CR_SERVER_LOST)
set_mysql_extended_error(mysql, CR_SERVER_LOST, unknown_sqlstate,
ER(CR_SERVER_LOST_EXTENDED),
"reading initial communication packet",
errno);
fprintf(myfp,"%s after cli_safe_read packet_error \n",cur_time());
goto error;
}
线程阻塞时抓到的堆栈如下:
#0 0x0000003ebe0c5f3b in read () from /lib64/libc.so.6
#1 0x00007f240dd91430 in vio_read () from /usr/lib/libmysql.so.16.0.0
#2 0x00007f240dcf9b05 in my_real_read () from /usr/lib/libmysql.so.16.0.0
#3 0x00007f240dcf9e38 in my_net_read () from /usr/lib/libmysql.so.16.0.0
#4 0x00007f240dcec396 in cli_safe_read () from /usr/lib/libmysql.so.16.0.0
#5 0x00007f240dceea17 in mysql_real_connect () from /usr/lib/libmysql.so.16.0.0

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

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

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



The article brought to you in this chapter is about the NavicatforMySQL software. Do you know how NavicatforMySQL connects to the local MySQL database? Then, the editor brings you the method of NavicatforMySQL to connect to the local MySQL database. Interested users can read below. Take a look. Open the computer where Navicatformysql has been installed, and then click the "Connect" option in the upper right corner. In the pop-up new connection window, you can enter the connection name and set the host name to the local database, so just use "localhost", Just leave the password blank. Then if the connection to the convenient database is successful,

After using Docker to deploy MySQL, the connection speed is slow. Through online searches, I found that the problem may be caused by the lack of modules such as DNS resolution during the minimum container installation. Therefore, there will be a problem of super slow connection when connecting. We directly add this sentence skip-name-resolve and directly modify the docker-compose.yml configuration. The configuration is as follows version: "3" services: mysql: image: mysql: latestcontainer_name: mysql_composerestart: alwaysports:-3306:3306command:--default-a

Analysis of the Impact of MySQL Connection Number on Database Performance With the continuous development of Internet applications, databases have become an important data storage and management tool to support application systems. In the database system, the number of connections is an important concept, which is directly related to the performance and stability of the database system. This article will start from the perspective of MySQL database, explore the impact of the number of connections on database performance, and analyze it through specific code examples. 1. What is the number of connections? The number of connections refers to the number of client connections supported by the database system at the same time. It can also be managed

How to optimize the high concurrency performance of MySQL connections in a Python program? Abstract: MySQL is a relational database with powerful performance, but in the case of high concurrency, the connection and query operations of Python programs may affect the performance of the system. This article will introduce some optimization techniques to improve the performance of Python programs and MySQL databases. Use a connection pool: In high concurrency situations, frequently creating and closing database connections will consume a lot of system resources. Therefore, using connection pooling can effectively reduce

How to test the high concurrency performance of MySQL connections from the command line? With the continuous popularization of Internet applications, the high concurrency performance of databases has become one of the focuses of many demands. As a popular open source database, MySQL has also received widespread attention for its high concurrency performance. Before testing the high concurrency performance of MySQL connections, we need to clarify some concepts and preparations: Concurrent connections: refers to multiple clients establishing connections with the database at the same time, and these connections perform database operations at the same time. Connection limit: MySQ

How to deal with data loss after MySQL connection terminates abnormally? When using the MySQL database, sometimes the database connection will be terminated abnormally due to various reasons. This will not only cause the current operation to be interrupted, but may also cause the submitted data to be lost. In order to solve this problem, we need to take some measures to deal with data loss after the MySQL connection is terminated abnormally. First of all, we need to make it clear: MySQL is a database with transaction support. A transaction is a set of operations, either all submitted,

MySQL connection is reset, how to deal with it? MySQL is a commonly used relational database management system that is widely used in projects of various sizes. However, when using MySQL, we sometimes encounter situations where the connection is reset, which may cause some trouble for our project. This article will introduce why the MySQL connection is reset and how to deal with this problem. The connection being reset is usually caused by the following reasons: 1. Network problem: When the network connection between the client and the server connected to MySQL is unstable,

MySQL connection is reset, how to ensure the health of the connection pool through connection keepalive? When using the MySQL database, you often encounter situations where the connection is reset, causing the database connection to be interrupted, which is very unreliable for the application. In order to solve this problem, you can ensure the health of the connection pool by keeping the connection alive. Connection keep-alive means sending a specific query statement when the connection is idle to keep the connection active and prevent the connection from being actively closed by the server. This specific query statement can be a simple SE
