I have two servers with different purposes, one being a webserver running apache and php 5.3, and the other is a MySQL database server. The first uses the database in the second 24 hours a day, 7 days a week .
The problem is that we recently started having errors a few times a day, where PHP applications return the following error message to our users:
Can't connect to MySQL server on 'IP_DO_BANCO_DE_DADOS' (4)
We have several PHP applications in our WebServer, and the connection is executed according to the framework used. One of our applications (legacy) that does not use any framework, and therefore uses the natively PHP to connect to MySQL, proceed as follows:
function AbreBD($mysqli = false) {
global $conexao, $selecao, $conexao_mysqli;
$servidor = '192.168.101.20:3306';
$usuarioBD = 'user';
$senhaBD = 'user';
$conexao = mysql_connect($servidor, $usuarioBD, $senhaBD);
$selecao = mysql_select_db("bioextratus", $conexao);
if ($mysqli) {
$conexao_mysqli = new mysqli($servidor, $usuarioBD, $senhaBD, "bioextratus");
}
}
Remembering that the above code is for a legacy application, so it still uses the mysql driver instead of mysqli. Even in our most modern applications with frameworks, the problem also occurs.
I believe the problem is the MySQL configuration or network problem, although the MySQL service is always available, as well as the communication between the two servers over the network.
The content of the MySQL my.cnf file from our database server (without the comments):
[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock
[mysqld_safe]
socket = /var/run/mysqld/mysqld.sock
nice = 0
[mysqld]
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
skip-external-locking
explicit_defaults_for_timestamp = false
open_files_limit = 102400
bind-address = 0.0.0.0
key_buffer = 16M
key_buffer_size = 512M
innodb_buffer_pool_size = 256M
innodb_additional_mem_pool_size = 8M
max_allowed_packet = 16M
thread_stack = 196K
thread_cache_size = 8
myisam-recover-options = BACKUP
query_cache_limit = 64M
query_cache_type = 1
query_cache_size = 256M
max_connections = 1024
max_user_connections = 0
sort_buffer_size = 2M
join_buffer_size = 1M
tmp_table_size = 64M
max_heap_table_size = 18M
log_error = /var/log/mysql/error.log
expire_logs_days = 10
max_binlog_size = 100M
[mysqldump]
quick
quote-names
max_allowed_packet = 16M
[isamchk]
key_buffer = 12M