mysql insert多條數據(max_allowed_packet查詢和修改)

mysql根據配置文件會限制server接受的數據包大小。
有時候大的插入和更新會被max_allowed_packet 參數限制掉,導致失敗。
查看目前配置
show VARIABLES like ‘%max_allowed_packet%’;
顯示的結果爲:

+——————–+———+
| Variable_name | Value |
+——————–+———+
| max_allowed_packet | 1048576 |
+——————–+———+

以上說明目前的配置是:1M

修改方法
一、 方法1
可以編輯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

二、 方法2
(很妥協,很糾結的辦法)
進入mysql server
在mysql 命令行中運行
set global max_allowed_packet = 2*1024*1024*10
退出mysql命令行,然後重新登錄。
show VARIABLES like ‘%max_allowed_packet%’;
查看下max_allowed_packet是否編輯成功
注:方法2中,如果重啓mysql服務,max_allowed_packet的值會還原成默認的初始值,命令行裏設定的值不會生效。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章