#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>
int connect()
{
MYSQL *conn;
conn = mysql_init(NULL);
if (conn == NULL) {
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);
}
if (mysql_real_connect(conn, "localhost", "user", "pass", "sb", 0, NULL, 0) == NULL) {
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);
}
mysql_close(conn);
}
mysql + с
Error 2001: Can't create UNIX socket (24)
- mysql запущен и работает
- uname -a: Linux lolohost 2.6.28-11-server #42-Ubuntu
- cat /etc/issue.net: Ubuntu 9.04
- SELECT version(): 5.0.75-0ubuntu10
Код:
Это unix barfs on socket error. Нужно увеличить лимит на открытие файлов.
Допустим, пробуем так:
Цитата:
mysqlslap --concurrency=2000 --iterations=10 --number-int-cols=2 --number-char-cols=3 \
--auto-generate-sql --csv=/tmp/mysqlslap_q1000_innodb.csv --engine=innodb \
--auto-generate-sql-add-autoincrement --auto-generate-sql-load-type=mixed \
--number-of-queries=1000 --user=root --password=$PASSWD
mysqlslap: Error when connecting to server: 2001 Can't create UNIX socket (24)
mysqlslap: Error when connecting to server: 2001 Can't create UNIX socket (24)
...
ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
max nice (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 122944
max locked memory (kbytes, -l) 32
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
max rt priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 122944
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
[root@domU-12-31-39-00-00-61 ~]# ulimit -a mysql
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
max nice (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 122944
max locked memory (kbytes, -l) 32
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
max rt priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 122944
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
--auto-generate-sql --csv=/tmp/mysqlslap_q1000_innodb.csv --engine=innodb \
--auto-generate-sql-add-autoincrement --auto-generate-sql-load-type=mixed \
--number-of-queries=1000 --user=root --password=$PASSWD
mysqlslap: Error when connecting to server: 2001 Can't create UNIX socket (24)
mysqlslap: Error when connecting to server: 2001 Can't create UNIX socket (24)
...
ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
max nice (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 122944
max locked memory (kbytes, -l) 32
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
max rt priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 122944
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
[root@domU-12-31-39-00-00-61 ~]# ulimit -a mysql
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
max nice (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 122944
max locked memory (kbytes, -l) 32
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
max rt priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 122944
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
Устанавливаем лимит на открытые файлы до 4096:
Цитата:
ulimit -n 4096
ulimit -a mysql
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
max nice (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 122944
max locked memory (kbytes, -l) 32
max memory size (kbytes, -m) unlimited
open files (-n) 4096
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
max rt priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 122944
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
Results:
innodb,mixed,10.311,9.758,10.759,2000,0
ulimit -a mysql
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
max nice (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 122944
max locked memory (kbytes, -l) 32
max memory size (kbytes, -m) unlimited
open files (-n) 4096
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
max rt priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 122944
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
Results:
innodb,mixed,10.311,9.758,10.759,2000,0
Теперь для тестовых целей пробуем увеличить по 16384 (только для тестирования!):
Цитата:
ulimit -n 16384
mysql -u root -p$PASSWD
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 59768
Server version: 5.1.22-rc-community-log MySQL Community Edition (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> set global max_connections = 8192;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like '%open%';
+-------------------+----------+
| Variable_name | Value |
+-------------------+----------+
| have_openssl | DISABLED |
| innodb_open_files | 300 |
| open_files_limit | 8192 |
| table_open_cache | 2048 |
+-------------------+----------+
4 rows in set (0.00 sec)
mysql -u root -p$PASSWD
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 59768
Server version: 5.1.22-rc-community-log MySQL Community Edition (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> set global max_connections = 8192;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like '%open%';
+-------------------+----------+
| Variable_name | Value |
+-------------------+----------+
| have_openssl | DISABLED |
| innodb_open_files | 300 |
| open_files_limit | 8192 |
| table_open_cache | 2048 |
+-------------------+----------+
4 rows in set (0.00 sec)
И тестируем concurrency от 3000 до 8000:
Цитата:
mysqlslap --concurrency=3000,4000,5000,6000,7000,8000 --iterations=10 \
--number-int-cols=2 --number-char-cols=3 --auto-generate-sql --csv=/tmp/mysqlslap_q1000_innodb.csv \
--engine=innodb --auto-generate-sql-add-autoincrement --auto-generate-sql-load-type=mixed \
--number-of-queries=1000 --user=root --password=$PASSWD
--number-int-cols=2 --number-char-cols=3 --auto-generate-sql --csv=/tmp/mysqlslap_q1000_innodb.csv \
--engine=innodb --auto-generate-sql-add-autoincrement --auto-generate-sql-load-type=mixed \
--number-of-queries=1000 --user=root --password=$PASSWD
Работает. должно получиться.
подробнее здесь
Цитата:
1) Add line
* hard nofile 51200
to /etc/security/limits.conf
Note: The * means every user except root
Note: 51200 represents the number of concurrent open files. This number must by a multiple of 1024
2) Add line
session required pam_limits.so
to /etc/pam.d/common-session
3) Stop all processes that need the new limit
4) Restart SSH server
5) Login again
6) Run ulimit -a to check the values
7) If the values haven't changed, reboot and try step 6 again
8) Start new processes from command line
* hard nofile 51200
to /etc/security/limits.conf
Note: The * means every user except root
Note: 51200 represents the number of concurrent open files. This number must by a multiple of 1024
2) Add line
session required pam_limits.so
to /etc/pam.d/common-session
3) Stop all processes that need the new limit
4) Restart SSH server
5) Login again
6) Run ulimit -a to check the values
7) If the values haven't changed, reboot and try step 6 again
8) Start new processes from command line
но после выполнения всех танцев с бубном и ребута ulimit -n остается 1024
Код:
ulimit -v 102400
отпишитесь, что получится. Это должно быть persistence.
вообще говоря ничего)
значит запрашиваемое значение - слишком велико. попробуйте наращивать постепенно
оказалось что да, после sudo su и ulimit -n 8192 оно выставилось в 8192. Теперь SegFault...
Код:
#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>
int connect()
{
MYSQL *conn;
conn = mysql_init(NULL);
if (conn == NULL) {
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
return 0;
}
mysql_real_connect(conn, "localhost", "lolouser", "lolopasswd", "lolodb", 0, NULL, 0);
if (mysql_query(conn, "SELECT * FROM lol")) {
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);
}
mysql_close(conn);
}
#include <stdlib.h>
#include <mysql/mysql.h>
int connect()
{
MYSQL *conn;
conn = mysql_init(NULL);
if (conn == NULL) {
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
return 0;
}
mysql_real_connect(conn, "localhost", "lolouser", "lolopasswd", "lolodb", 0, NULL, 0);
if (mysql_query(conn, "SELECT * FROM lol")) {
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);
}
mysql_close(conn);
}
в выдаче имеем
Error 2006: MySQL server has gone away
Код:
MYSQL mysql;
mysql_init(&mysql);
if(!(mysql_real_connect(&mysql, "localhost", "username", "uspwd", NULL, 3306, NULL, 0)))
{
mysql_error(&mysql);
}
else
{
printf("Фсё пучком!");
//и от счастья давай вызывать наши рабочие методы
mysql_init(&mysql);
mysql_close(&mysql);
};
mysql_init(&mysql);
if(!(mysql_real_connect(&mysql, "localhost", "username", "uspwd", NULL, 3306, NULL, 0)))
{
mysql_error(&mysql);
}
else
{
printf("Фсё пучком!");
//и от счастья давай вызывать наши рабочие методы
mysql_init(&mysql);
mysql_close(&mysql);
};
Потестируйте на другом линухе или другой unix like OS. Хотя бы на виртуалке.