Linux

How to fix MySQL server has gone away on linux

The "MySQL server has gone away" error in MySQL typically occurs when the server closes the connection unexpectedly. This can be caused by several issues, such as server timing out connections, large packets, or resource limitations. Here are some common ways to troubleshoot and resolve this problem on a Linux system:

1. Increase the `wait_timeout` and `interactive_timeout`

The error might be due to the server timing out the connection too quickly. You can increase the `wait_timeout` and `interactive_timeout` settings in your MySQL configuration (`my.cnf` or `my.ini` file), typically located in `/etc/mysql/`.

[mysqld]
wait_timeout = 28800
interactive_timeout = 28800

After making these changes, restart the MySQL server:
```bash
sudo systemctl restart mysql
```

2. Check and Increase `max_allowed_packet`

This error can also happen if a packet larger than `max_allowed_packet` is sent to MySQL. You can increase this setting:

[mysqld]
max_allowed_packet=64M

Again, restart the MySQL server after making this change.

3. Server Restart Issues

If your server restarts unexpectedly or is killed without a proper shutdown, this could lead to temporary or intermittent "MySQL server has gone away" errors. Ensure your server is configured to handle your workload and that it isn't being stopped by the operating system to conserve memory or for other reasons.

4. Network Issues

Check your network settings if MySQL and the application are on different hosts. Network interruptions can cause this error. Consider increasing `net_read_timeout` and `net_write_timeout` if network delays are suspected:

[mysqld]
net_read_timeout=600
net_write_timeout=600

5. Check MySQL Server Logs

Examine the MySQL server logs for any warnings or errors that can provide more insights. The logs can usually be found in `/var/log/mysql/` or as defined in your MySQL configuration file. Look for messages that indicate why the server connection was dropped.

6. Persistent Connections

If you're using persistent connections in your application, ensure they are properly managed and not causing the server to reach its connection limit.

7. Upgrade MySQL

If you are running an older version of MySQL, consider upgrading to a newer version. Newer versions may have fixes for bugs that cause this error.

 

My file my.cnf you can copy it:

# The MySQL server
[mysqld]
port        = 3306
socket      = /tmp/mysql.sock
skip-locking
key_buffer_size = 16M
max_allowed_packet = 512M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
wait_timeout = 6000

After making any configuration changes, ensure you restart the MySQL service to apply them. Always back up your configuration files before making significant changes.

systemctl restart mysql 

Thanks for visit my website