php mysqli 一次插入10w条数据方法 uery(): MySQL server has gone away

<?php
	
$db_host='localhost';
$db_user='root';
$db_password='root';
$db_name='demo';
$mysqli = new mysqli($db_host, $db_user, $db_password, $db_name); 


// $sql="INSERT INTO `demo` (`name`) VALUES (now())";

 for ($i=1; $i<=100000;$i++) {
  $password = $i;
  $username = 'user--'.$i;
  $sql[] = "('$username','$password')";
 }
 // var_dump($sql);
 // echo implode(",",$sql);
 // die;
 // mysql_query('insert into demo(id,username) values '.implode(',',$sql));

$result=$mysqli->query('insert into user (username,password) values '.implode(',',$sql));
if ($result){
         echo "执行成功".$stop-$starttime;
}else {
         echo "执行失败";
}
$mysqli->close();


因为自己要测试用,10w条数据插入

当开始插入1w或者5w的时候,都可以正常插入。(本地环境phpstudy)


一次插入10w的时候,uery(): MySQL server has gone away提示。

主要修改'max_allowed_packet'; 参数,


mysql> show global status like 'uptime';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Uptime        | 89893 |
+---------------+-------+
1 row in set (0.04 sec)


mysql> show global variables like '%timeout';
+----------------------------+----------+
| Variable_name              | Value    |
+----------------------------+----------+
| connect_timeout            | 10       |
| delayed_insert_timeout     | 300      |
| innodb_lock_wait_timeout   | 50       |
| innodb_rollback_on_timeout | OFF      |
| interactive_timeout        | 28800    |
| lock_wait_timeout          | 31536000 |
| net_read_timeout           | 30       |
| net_write_timeout          | 60       |
| slave_net_timeout          | 3600     |
| wait_timeout               | 28800    |
+----------------------------+----------+
10 rows in set (0.00 sec)


mysql> show global variables like 'max_allowed_packet';
+--------------------+---------+
| Variable_name      | Value   |
+--------------------+---------+
| max_allowed_packet | 1048576 |
+--------------------+---------+
1 row in set (0.00 sec)


mysql> set global max_allowed_packet='1024*0124*16';
ERROR 1232 (42000): Incorrect argument type to variable 'max_allow
mysql> set global max_allowed_packet=1024*1024*16;
Query OK, 0 rows affected (0.00 sec)


mysql> show global variables like 'max_allowed_packet';
+--------------------+----------+
| Variable_name      | Value    |
+--------------------+----------+
| max_allowed_packet | 16777216 |
+--------------------+----------+
1 row in set (0.00 sec)


mysql>


mysql> select count(*) from user;
+----------+
| count(*) |
+----------+
|    50000 |
+----------+
1 row in set


mysql> select count(*) from user;
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set


mysql> select count(*) from user;
+----------+
| count(*) |
+----------+
|   100000 |
+----------+
1 row in set


















































































































































































































































































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值