How to update the max_connections in MySQL

ปัญหาการเกิด error “too many connections” ในขณะที่ query database อยู่ เกิดจากจำนวน connections ที่เข้ามาติดต่อ database มีจำนวนมากกว่าที่ max_connections ได้กำหนดไว้ ซึ่งโดยปกติแล้ว MySQL จะยอมให้มีจำนวน connections ได้เท่ากับ max_connections+1 โดยที่ max_connections คือ จำนวน connections ที่มากที่สุดที่อนุญาต และ +1 คือ connection ของ super user

ค่า default ของ max_connections สำหรับ version ที่ต่ำกว่า 5.1.15 จะเท่ากับ 100 connections ถ้าสูงกว่ามีค่าเท่ากับ 151 connections และค่าสูงสุดที่สามารถกำหนดได้ สำหรับ version 5.1.15 – 5.1.16 คือ 16384 แต่ถ้า version 5.1.17 เป็นต้นไปสามารถกำหนดได้ถึง 100000

คำสั่งที่ใช้ในการตรวจสอบค่า max_connections (ต้องอยู่ใน MySQL command line tool)

mysql> show variables like "max_connections";
+------------------------+---------+
| Variable_name     | Value |
+------------------------+---------+
| max_connections | 300   |
+------------------------+---------+
1 row in set (0.00 sec)

วิธีการกำหนดค่า max_connections สามารถทำได้ 2 วิธีดังนี้

  1. การใช้ command line สำหรับวิธีการนี้เป็นการกำหนดค่าแบบชั่วคราวโดยไม่ต้อง restart แต่ต้อง login ไปยัง MySQL command line tool ก่อน
    ตัวอย่างกำหนดให้จำนวน connections มากที่สุดเท่ากับ 300

    mysql> set global max_connections = 300;
  2. แก้ไขไฟล์ /etc/my.cnf
    ให้ใส่ค่าจำนวน max_connections ที่ต้องการใน section [mysqld] ดังตัวอย่าง

    [mysqld]
    max_connections = 300  //กำหนดให้จำนวน connections มากที่สุดเท่ากับ 300

การกำหนดค่า max_connections จะมีการใช้ RAM มากขึ้นด้วยดังนั้นจึงไม่ควรเพิ่มมากเกินความจำเป็น โดยที่เหมาะสมของ Linux หรือ Solaris อยู่ระหว่าง 500 ถึง 1000 แต่ถ้าคุณเหลือ RAM จำนวนมาก (> 1GB) ก็สามารถกำหนดมากกว่านี้ได้

Related posts:

  1. ติดตั้ง MySQL Cluster ในบทความนี้จะใช้เครื่องจำนวน 4 เครื่อง โดยแต่ละเครื่องจะมีหน้าที่ storages node จำนวน 2...
  2. MySQL replication: ‘show master status’ shows ‘Empty set (0.00 sec)’ หลังจากได้เตรียม server สำหรับ MySQL replication ด้วยการทำให้ข้อมูลของทั้งสองเครื่องเหมือนกันเรียบร้อยแล้ว ก็มาถึงขั้นตอนการ setup...
  3. View extended information about network connections using netstat? เราสามารถใช้คำสั่ง netstat ดูรายละเอียดต่างๆของ network ได้โดยใช้คำสั่งข้างล่างนี้ [root@example ~]# netstat...
  4. แนะนำ Putty Manager tool สำหรับสาวก putty ผมมี tool ตัวใหม่มาแนะนำ ซึ่ง tool ตัวนี้มีประโยชน์สำหรับคนที่ต้อง...
  5. Linux Cluster Concept [Fail-Over and Load-Balancing Cluster] Linux Cluster style 1. High Availability or Fail-Over...

You can skip to the end and leave a response. Pinging is currently not allowed.

Leave a Reply