使用shell脚本向mysql批量插入数据

为什么80%的码农都做不了架构师?>>>   hot3.png

很多时候需要在mysql表中插入大量测试数据,写一个java程序来插入太麻烦,但是写一个简单的shell脚本向mysql插入数据却很方面。

  1. 在mysql中新建表,如下:
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(200) DEFAULT NULL,
  `createtime` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
  1. 到安装mysql的服务器,创建generate.sh脚本,定时插入数据,内容如下:
#!/bin/bash
i=1
while [ $i -le 100000000 ]
do
    mysql -uroot -p123456 test -e "insert into student (name,createTime) values ('student$i',NOW());"
    i=$(($i+1))
    sleep 6
done
  1. 启动脚本,插入数据,命令如下:nohup sh ./generate.sh &

参考:用shell脚本在mysql表中批量插入数据的方法

转载于:https://my.oschina.net/cjun/blog/779658

你可能感兴趣的:(使用shell脚本向mysql批量插入数据)