mysql max_allowed_packet查询和修改

来源:互联网 发布:三轴点胶机编程教学 编辑:程序博客网 时间:2024/05/23 01:14
mysql根据配置文件会限制server接受的数据包大小。
有时候大的插入和更新会被max_allowed_packet 参数限制掉,导致失败。
查看目前配置 
show VARIABLES like '%max_allowed_packet%';
显示的结果为:
 
+--------------------+---------+
| Variable_name      | Value   |
+--------------------+---------+
| max_allowed_packet | 1048576 |
+--------------------+---------+
 
以上说明目前的配置是:1M
 
修改方法
可以编辑my.cnf来修改(windows下my.ini),在[mysqld]段或者mysql的server配置段进行修改。
max_allowed_packet = 20M
如果找不到my.cnf可以通过
mysql --help | grep my.cnf
去寻找my.cnf文件。
[root@localhost usr]# mysql --help | grep my.cnf
                      order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf

在linux下会发现上述文件可能都不存在。
1)先确定出使用的配置文件的路径(如果未启动,可先启动)
[root@localhost usr]# ps aux |grep mysql
root     14688  0.0  0.0  11336  1404 pts/0    S    19:07   0:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/localhost.localdomain138.pid
mysql    14791  0.0 15.4 1076700 451336 pts/0  Sl   19:07   0:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/lib/mysql/localhost.localdomain138.err --pid-file=/var/lib/mysql/localhost.localdomain138.pid
root     14835  0.0  0.0 201584  2504 pts/0    S+   19:09   0:00 mysql -u root -p
root     15143  0.0  0.0 103244   828 pts/1    S+   19:40   0:00 grep mysql
 找见mysqld或mysqld_safe的那一行,看下basedir=/path/file ,那个/path/file就是配置文件路径;
2)也可以直接创建 /etc/my.cnf, 或者从你安装的mysql的相关目录中(可能是/usr/include/mysql或/usr/share/mysql)找一个my.cnf 或 my-small.cnf 拷贝为/etc/my.cnf,mysql启动时会优先使用这个配置文件。
可以用如下命令在/etc目录下查找my.cnf类似的文件名:
[root@localhost usr]# find -name "my*.cnf"
./my.cnf
./share/mysql/my-default.cnf
./share/doc/MySQL-server-5.6.16/my-default.cnf
./my-new.cnf
3)有了配置文件,在配置文件中的[mysqld]下边加些常用的配置参数。重启mysql服务器后,该参数即可生效。

  max_allowed_packet=32M


---------->>案例:


刚才在导入数据库的时候 SQLyog 报错了

Error Code: 2006 - MySQL server has gone away

搜了下,说是max_allowed_packet (MySQL的一个参数)设置的值不够大。

那我改下就行了 嘿嘿

In Windows:

In the MySQL server installation directory,
in my.ini file, add the following line under [mysqld] in SERVER SECTION.

max_allowed_packet = 16M


In Linux:

Copy the my-xxx.cnf file from /usr/share/mysql to /etc as my.cnf

xxx can be small, medium, large, huge ... depending on the requirement.

$ cp /usr/share/mysql/my-xxx.cnf /etc/my.cnf

In the my.cnf file, change the default
max_allowed_packet = 1M
to
max_allowed_packet = 16M

Save the file and restart MySQL server.

文章出处

0 0