<?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