MYSQL 数据库安装完成后,默认最大连接数是100,一般流量稍微大一点的论坛或网站这个连接数是远远不够的,虽然通过连接池可以将连接数设置为固定的100,可 是在下那BT同事将所有的连接池都设置成了100,话说我们小组有5个人……再话说,个人调试有必要设置那么高么……不过牢骚归牢骚,问题依然需要解决。
方 法一:进入MYSQL安装目录 打开MYSQL配置文件 my.ini 或 my.cnf查找 max_connections=100 修改为 max_connections=1000 服务里重起MYSQL即可。此方法用于Windows下的mysql设置,简单方便。如果是Linux的话,请看下面。
方法二:MySQL的最大连接数默认是100
客户端登录:mysql -uroot -p
设置新的最大连接数为1000:mysql> set GLOBAL max_connections=1000
显示当前运行的Query:mysql> show processlist
显示当前状态:mysql> show status
退出客户端:mysql> exit
查看当前最大连接数:mysqladmin -uroot -p variables
这个办法治标不治本,重启服务以后最大连接数还是100。这个方法用于紧急扩充最大连接数用,不是长久之计。
方法三:以centos 5.4 下面的mysql 5.0.83 rpm版本为例说明:
找到/usr/bin/mysqld_safe编辑它,找到mysqld启动的那两行,在后面加上参数 :
-O max_connections=1000
用红字特别说明:
if test -z "$args"
then
$NOHUP_NICENESS $ledir/$MYSQLD $defaults --basedir=$MY_BASEDIR_VERSION
--datadir=$DATADIR $USER_OPTION --pid-file=$pid_file
--skip-external-locking -O max_connections=1000 >> $err_log 2>&1
else
ev al
"$NOHUP_NICENESS $ledir/$MYSQLD $defaults --basedir=$MY_BASEDIR_VERSION
--datadir=$DATADIR $USER_OPTION --pid-file=$pid_file
--skip-external-locking $args -O max_connections=1000 >> $err_log 2>&1"
fi
这段代码很不好找,在下也无法说清楚它具体在哪个位置。不过建议从最后往前找会比较快。
重启mysql服务:# service mysql restart
查看当前最大连接数:# /usr/local/mysql/bin/mysqladmin -uroot -p variables
输入root数据库账号的密码后可看到
max_connections 1000 即新改动已经生效。
第四种方法:修改mysqld的启动文件/etc/init.d/mysqld 在start()函数末尾添加代码
mysql -e ‘set GLOBAL max_connections=1000;‘如
start(){
[ -x $exec ] || exit 5
# check to see if it‘s already running
RESPONSE=`/usr/bin/mysqladmin --socket="$socketfile" --user=UNKNOWN_MYSQL_USER ping 2>&1`
if [ $? = 0 ]; then
# already running, do nothing
action $"Starting $prog: " /bin/true
ret=0
elif echo "$RESPONSE" | grep -q "Access denied for user"
then
# already running, do nothing
action $"Starting $prog: " /bin/true
ret=0
else
# prepare for start
touch "$errlogfile"
chown mysql:mysql "$errlogfile"
chmod 0640 "$errlogfile"
[ -x /sbin/restorecon ] && /sbin/restorecon "$errlogfile"
if [ ! -d "$datadir/mysql" ] ; then
# First, make sure $datadir is there with correct permissions
if [ ! -e "$datadir" -a ! -h "$datadir" ]
then
mkdir -p "$datadir" || exit 1
fi
chown mysql:mysql "$datadir"
chmod 0755 "$datadir"
[ -x /sbin/restorecon ] && /sbin/restorecon "$datadir"
# Now create the database
action $"Initializing MySQL database: " /usr/bin/mysql_install_db --datadir="$datadir" --user=mysql
ret=$?
chown -R mysql:mysql "$datadir"
if [ $ret -ne 0 ] ; then
return $ret
fi
fi
chown mysql:mysql "$datadir"
chmod 0755 "$datadir"
# Pass all the options determined above, to ensure consistent behavior.
# In many cases mysqld_safe would arrive at the same conclusions anyway
# but we need to be sure. (An exception is that we don‘t force the
# log-error setting, since this script doesn‘t really depend on that,
# and some users might prefer to configure logging to syslog.)
# Note: set --basedir to prevent probes that might trigger SELinux
# alarms, per bug #547485
$exec --datadir="$datadir" --socket="$socketfile" \
--pid-file="$mypidfile" \
--basedir=/usr --user=mysql >/dev/null 2>&1 &
safe_pid=$!
# Spin for a maximum of N seconds waiting for the server to come up;
# exit the loop immediately if mysqld_safe process disappears.
# Rather than assuming we know a valid username, accept an "access
# denied" response as meaning the server is functioning.
ret=0
STARTTIMEOUT=120
while [ $STARTTIMEOUT -gt 0 ]; do
RESPONSE=`/usr/bin/mysqladmin --socket="$socketfile" --user=UNKNOWN_MYSQL_USER ping 2>&1` && break
echo "$RESPONSE" | grep -q "Access denied for user" && break
if ! /bin/kill -0 $safe_pid 2>/dev/null; then
echo "MySQL Daemon failed to start."
ret=1
break
fi
sleep 1
let STARTTIMEOUT=${STARTTIMEOUT}-1
done
if [ $STARTTIMEOUT -eq 0 ]; then
echo "Timeout error occurred trying to start MySQL Daemon."
ret=1
fi
if [ $ret -eq 0 ]; then
action $"Starting $prog: " /bin/true
touch $lockfile
else
action $"Starting $prog: " /bin/false
fi
fi
mysql -e ‘set GLOBAL max_connections=1000;‘
return $ret
}