标签:意义 sql 127.0.0.1 vim命令 shell 数据 mys mysq password
经常要清空数据库,于是写了一个小脚本,还是学到很多东西.
mysqldump -u${USERNAME} -p${PASSWORD} -h${HOSTNAME} ${DATABASE} --no-data >villadb-nodata.sql
mysql -u${USERNAME} -p${PASSWORD} -h${HOSTNAME} ${DATABASE} -e "drop database ${DATABASE}"
mysql -u${USERNAME} -p${PASSWORD} -h${HOSTNAME} -e "create database ${DATABASE}"
学习点:
1.mysql -e cmd 可以用shell操作mysql,不在mysql提示符下操作,非交互式,适合嵌入shell脚本
2.shell 单引号和双引号的区别,单引号属于强引用,不对引号内任何特殊字符转义,只当普通字符.双引号属于弱引用,会对括起来的特殊字符做处理.如$,>等.上面的例子用到${DATABASE},要用双引号,才能保留${}的特殊意义,即传递一个变量,而不是简单字符串.
3.mysqldump --no-data 参数(跟-d 一样),表示只导出数据库表结构,不导出数据.用-t则表示只导出数据,不导出表结构.
4.删除数据库有3种方法:
drop database ${DATABASE}:会删除整个数据库表结构,数据,和
取消注释:在块模式下选中要取消注释的行,按一下"d",即可
取消注释:在命令行模式,输入":1,$ s/^#//g".
#!/usr/bin/bash
set -x
HOSTNAME=‘127.0.0.1‘
USERNAME=‘root‘
PASSWORD=‘engine‘
DATABASE=‘villadb‘
result()
{
if [ $? -eq 0 ];then
echo -e "\033[47;32m ----------- $1 succeed! ---------- \033[0m"
else
echo -e "\033[47;31m ----------- $1 failed! ----------- \033[0m"
fi
}
echo -e "\033[47;34m ------------ step 1: dump a no-data sql of villadb ------------- \033[0m"
mysqldump -u${USERNAME} -p${PASSWORD} -h${HOSTNAME} ${DATABASE} --no-data >villadb-nodata.sql
result step1
echo -e "\033[47;34m ------------ step 2: dump important config like VDC,ServiceOffering,etc ------------- \033[0m"
mysqldump -u${USERNAME} -p${PASSWORD} -h${HOSTNAME} ${DATABASE} VdcType ServiceOffering TotalTemplate VDC Configuration > config.sql
result step2
echo -e "\033[47;34m ------------ step 3: drop villadb ------------- \033[0m"
mysql -u${USERNAME} -p${PASSWORD} -h${HOSTNAME} ${DATABASE} -e ‘drop database villadb‘
result step3
echo -e "\033[47;34m ------------ step 4: create a new villadb ------------- \033[0m"
mysql -u${USERNAME} -p${PASSWORD} -h${HOSTNAME} -e ‘create database villadb IF NOT EXISTS villadb‘
result step4
echo -e "\033[47;34m ------------ step 5: import the backup table-structure ------------- \033[0m"
mysql -u${USERNAME} -p${PASSWORD} -h${HOSTNAME} ${DATABASE} -e ‘source /home/clouder/userful-sql/villadb-nodata.sql‘
result step5
echo -e "\033[47;34m ------------ step 6: import the backup config ------------- \033[0m"
mysql -u${USERNAME} -p${PASSWORD} -h${HOSTNAME} ${DATABASE} -e ‘source /home/clouder/userful-sql/config.sql‘
result step6
标签:意义 sql 127.0.0.1 vim命令 shell 数据 mys mysq password
原文地址:https://www.cnblogs.com/xiaozhuangAna/p/10348146.html