1、LOAD DATA通过读取本地文件系统上的文件,可以将大量数据添加到数据库中。
mysql>LOAD DATA INFILE ‘datafile.txt’ INTO TABLE db2.table_name
mysql>LOAD DATA INFILE ‘datafile.txt’ INTO TABLE db2.table_name FIELDS TERMINATED BY ‘,’ OPTIONALLY ENCLOSED BY ‘”‘
mysql>load data infile “file.txt” into table table_name fields terminated by ‘\t’ (sid,name)
2、还有一个mysqlimport命令可以批量增加,mysqlimport直接从文件读取批量数据。它相当于LOAD DATA语句的一个接口。
mysqlimport可以自动生成一个LOAD DATA语句,该语句把filename.txt文件中的数据装入table_name表中。
mysqlimport根据文件名导入表名,即将文件名第一个圆点前的所有字符作为表名。例如,文件class.txt被装入class表中。
例如:
mysqlimport -L -uroot -proot db01 table_name.txt;
mysqlimport -local table_name filename.txt;
3、datafile.txt内容:
“1”,”a string”,”100.20″
“2”,”a string containing a , comma”,”102.20″
“3”,”a string containing a \” quote”,”102.20″
“4”,”a string containing a \”, quote and comma”,”102.20″
4、假如你有x表,导入了一个y表,将y表数据插入x表:
insert into x select * from y
如果您觉得文本对您有帮助,请打赏,谢谢。
新建执行脚本:mysql_install.sh,并添加执行权限
#!/bin/bash
#zhouyihua V0.1 2021.07.08
#For centos 7.0
# add DNS
echo "---------- Add DNS --------"
echo "nameserver 8.8.8.8" >>/etc/resolv.conf
#stop firewall
systemctl stop firewalld
if [ $? -ne 0 ]then
echo "Firewall stop failed"
else
echo "Firewall stop success"
fi
#download mysql yum source
wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
#install mysql yum source
rpm -ivh mysql80-community-release-el7-3.noarch.rpm
#enable 5.7
sed -i '21s/enabled=0/enabled=1/g' /etc/yum.repos.d/mysql-community.repo
sed -i '21s/enabled=0/enabled=1/g' /etc/yum.repos.d/mysql-community.repo
#disable 8.0
sed -i '28s/enabled=1/enabled=0/g' /etc/yum.repos.d/mysql-community.repo
#install mysql
yum -y remove mariadb-libs
yum install -y mysql-community-server
#config mysql
echo "----------- Config my.cnf ----------"
sed -i '/\[mysqld\]/a\lower_case_table_names=1' /etc/my.cnf
sed -i '/\[mysqld\]/a\skip-grant-tables' /etc/my.cnf
#start mysql
echo "---------- Starting mysql service ----------"
systemctl start mysqld.service
mysql -uroot -p123456 <<EOF
use mysql
update mysql.user set authentication_string=password('Oracle1234!@#$') where user='root'
EOF
echo "---------- Annotation skip grant tables in my.cnf ----------"
sed -i 's/skip-grant-tables/\#skip-grant-tables/g' /etc/my.cnf
echo "---------- Restart mysql service ----------"
systemctl restart mysqld.service
echo "---------- Grant all privileges ----------"
mysql -uroot -p'Oracle1234!@#$' --connect-expired-password -e "alter user 'root'@'localhost' identified by 'Oracle1234!@#$'"
mysql -u root -p'Oracle1234!@#$' <<EOF
use mysql
GRANT ALL PRIVILEGES ON *.* TO 'iam'@'%' IDENTIFIED BY 'Oracle1234!@#$'
EOF
#!/bin/bashdate=$(date -d '+0 days' '+%Y-%m-%d') #声明一个变量,并赋值当天日期
cd /root/data #进入目录
mkdir $date #创建目录
mysqldump -uusername -ppassword db_name > $date"db_name.sql" #数据库备份,修改用户名密码,还有要备份数据库
tar -zcf $date".tar.gz" $date #压缩打包
rm -rf $date #删除源数据
保存为bak.sh,每天16:30备份定时任务,没有crontab命令:yum install vixie-cron
chmod +x /.../bak.sh
crontab -e
30 16 * * * /.../bak.sh
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)