-1

I have many PHP-Instances (250 to 500 and more), which open a DB-Connection and do SELECT's, UPDATE's and INSERT's. After few seconds i get the following Error:

PHP Fatal error:  Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (4)' in /var/www/xxx/vendor/propel/propel/src/Propel/Runtime/Connection/PdoConnection.php:49

I spend at least 30 hours to find a solution for that. In my opinion, the fault lies in a wrong configuration of MySQL or Debian. I found various configuration-options, but none of them work.


Additional informations

  • The error only occurs on remote servers - when I start the scripts on the DB-Server directly, i get no errors.

Current configs

sysctl.conf

net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
# Disable netfilter on bridges.
#net.bridge.bridge-nf-call-ip6tables = 0
#net.bridge.bridge-nf-call-iptables = 0
#net.bridge.bridge-nf-call-arptables = 0

net.ipv4.ip_forward = 1
net.ipv4.neigh.default.gc_thresh1 = 4096
net.ipv4.neigh.default.gc_thresh2 = 8192
net.ipv4.neigh.default.gc_thresh3 = 16384
net.ipv4.neigh.default.gc_interval = 5
net.ipv4.neigh.default.gc_stale_time = 120
net.core.netdev_max_backlog = 262144
#net.core.rmem_default = 16777216
net.core.rmem_max = 108544
net.core.somaxconn = 32768
net.core.wmem_max = 108544

net.netfilter.nf_conntrack_max = 10000000
net.netfilter.nf_conntrack_tcp_timeout_established = 40
net.netfilter.nf_conntrack_tcp_timeout_close = 10
net.netfilter.nf_conntrack_tcp_timeout_close_wait = 10
net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 10
net.netfilter.nf_conntrack_tcp_timeout_last_ack = 10
net.netfilter.nf_conntrack_tcp_timeout_syn_recv = 10
net.netfilter.nf_conntrack_tcp_timeout_syn_sent = 10
net.netfilter.nf_conntrack_tcp_timeout_time_wait = 10

net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_max_orphans = 262144
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_sack = 0
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syncookies = 0
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_wmem = 4096 16384 16777216

etc/mysql/my.cf

[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
lc-messages-dir = /usr/share/mysql
skip-external-locking

bind-address        = {local server ip}

key_buffer      = 16M
max_allowed_packet  = 16M
thread_stack        = 192K
thread_cache_size       = 1100
myisam-recover         = BACKUP
max_connections        = 1000
#table_cache            = 64
#thread_concurrency     = 10

query_cache_limit   = 2G
query_cache_size        = 32M

#general_log_file        = /var/log/mysql/mysql.log
#general_log             = 1
log_error = /var/log/mysql/error.log
slow_query_log_file = /var/log/mysql/mysql-slow.log
slow_query_log      = 1
long_query_time = 2
log_queries_not_using_indexes
#server-id      = 1
#log_bin            = /var/log/mysql/mysql-bin.log
expire_logs_days    = 10
max_binlog_size         = 100M
#binlog_do_db       = include_database_name
#binlog_ignore_db   = include_database_name

innodb_buffer_pool_size = 16G
innodb_buffer_pool_instances = 16

[mysqldump]
quick
quote-names
max_allowed_packet  = 16M

[mysql]
#no-auto-rehash # faster start of mysql but no tab completition

[isamchk]
key_buffer      = 16M

!includedir /etc/mysql/conf.d/

load modules - modules.conf

nf_conntrack
nf_conntrack_ipv4

script start on startup

echo "1" > /proc/sys/net/ipv4/tcp_tw_reuse
echo "1" > /proc/sys/net/ipv4/tcp_tw_recycle
echo "15" > /proc/sys/net/ipv4/tcp_fin_timeout
/etc/init.d/networking restart

My System

  • Debian 8.2
  • MySQL 5.5.44-0+deb8u1
  • php5-mysql 5.6.13+dfsg-0+deb8u1
  • Virtual Machine with 64GB RAM, 48 CPU's (Intel Xeon E7540 at 2.0 GHz)
  • Run at an ESxi-Host v. 6.0.0 2494585

load Testing

start x PHP-instances / loadTester.php

<?php
    require_once __DIR__ . "/autoloader.php";

    $numberOfInstances = 100;
    $index = 0;
    $numberOfDataset = 500;
    while($index < $numberOfInstances) {
        @system( "php " . __DIR__ . "/loadTest.php $index $numberOfDataset > /dev/null &" );

        $index++;
    }

one instance SELECT / loadTest.php

<?php
    require_once __DIR__ . "/autoloader.php";

    $skip = $argv[ 1 ];
    $numberOfDataset = $argv[ 2 ];

    $loops = 100;
    $index = 0;
    $skip = $skip * $numberOfDataset;
    while ( $index < $loops ) {
        $adresses = AddressQuery::create()->offset( $skip )->limit( $numberOfDataset )->find();
    }

Network Diagram

Network Diagram

Community
  • 1
  • 1
  • 1
    Can you look at the Processlist from th running DB "SHOW PROCESSLIST" to see you mutch connections are realy used in this moment – Bernd Buffen Sep 19 '15 at 08:43
  • @BerndBuffen: After start "loadTester.php" with 100 instances I have 57 Sleeps and 63 Query's (Writing to net)-connections. 34 PHP-Process stoped already cause the exception. – gordenfreeman1991 Sep 19 '15 at 08:56
  • I am nearly sure that there is no error in the my.cnf (only for optimize). The only test for the db is a smal bash script. for i in {1..1000}; do echo $i; mysql -uroot -pxxx -e "select sleep(300)"&; done -- is this running so the DB is OK and the error must in the VM – Bernd Buffen Sep 19 '15 at 09:07
  • I startet the 980-loop-script direct on the server - no error. I started it again - 1000 opened connection's, for the rest i get the error "ERROR 1040 (HY000): Too many connections" - that's correct, in the MySQL-config is max_connection set to 1000. **BUT** i startet de script on a remote server - and the "Can't connect to MySQL server on 'xxx.xxx.xxx.xxx'"-Error occurs after 868 connections. After killing the process and starting it again the Error occurs after 423 connections... Probably its a TCP-Connection-setting? – gordenfreeman1991 Sep 19 '15 at 10:29
  • This is also my opinion, but there i cant help you, sorry - good luck – Bernd Buffen Sep 19 '15 at 10:42
  • :o) yes i need a Debian and/or MySQL-Guru. Anyway, thank you for your help – gordenfreeman1991 Sep 19 '15 at 10:53
  • if have found this: http://stackoverflow.com/questions/410616/increasing-the-maximum-number-of-tcp-ip-connections-in-linux – Bernd Buffen Sep 19 '15 at 10:57

1 Answers1

0

I found a solution for the "probably TCP/IP-Problem":

I created a SSH-Tunnel to the MySQL-Server - the remote-Server now has a 10 times lower CPU load, the error no longer appears and the connection is now encrypted.

Setting on the remote server

I created a bash-script, that runs on every startup:

ssh -fNg -L 3307:{local server ip}:3306 -p {redirect port} root@{public ip}

On my router I configured a forwarding to the virtual database server -> {public ip} {redirect port}