SET GLOBAL max_allowed_packet doesn't work

2019-02-01 07:28发布

问题:

I found out how to change the default value of max_allowed_packet in MySQL using SET GLOBAL. However, each time I used this command, the default value stayed untouched! I used these commands:

mysql --user=root --password=mypass
mysql> SET GLOBAL max_allowed_packet=32*1024*1024;
Query OK, 0 rows affected (0.00 secs)
mysql> SHOW VARIABLES max_allowed_packet;

And then the result is max_allowed_packet = 1048576. What am I missing?

回答1:

Hmmmm.. You have hit this NOT-A-BUG it seems. :)

If you change a global system variable, the value is remembered and used for new connections until the server restarts. (To make a global system variable setting permanent, you should set it in an option file.) The change is visible to any client that accesses that global variable. However, the change affects the corresponding session variable only for clients that connect after the change. The global variable change does not affect the session variable for any client that is currently connected (not even that of the client that issues the SET GLOBAL statement).

Refer this too. Read Shane Bester explanation.

You should change from the my.ini/my.cnf file and restart the server for the max_allowed_packet setting to take effect.



回答2:

After running

set global max_allowed_packet=1000000000;

you have to restart mysql before

SHOW VARIABLES LIKE 'max_allowed_packet'

will show the new value.

I have this issue when restarting mysql through the MAC OSX system preferences and the value hadn't changed. So by logging into mysql via console

mysql -u root -p

changing it and then restarting mySql seemed to work. Might have been a OS X quirk though.



回答3:

Just a quick way to see the variable for anybody who comes across this. To get the value back you need to run

SHOW VARIABLES LIKE 'max_allowed_packet'