shell脚本执行mysql基本命令

#!/bin/bash

#database initial 
DB_HOST="localhost"
DB_USER="root"
DB_PASSWD="123456"
PORT="3306"

#database name
DB_NAME="test"

#table name
TABLENAME1="table1"
TABLENAME2="table2"

#create database
create_db_sql="create database  ${DB_NAME}"
mysql -h${DB_HOST}  -P${PORT}  -u${DB_USER} -p${DB_PASSWD} -e "${create_db_sql}"

#create tables 
# ID  Data  
create_table_sql_1="create table ${TABLENAME1} (ID int(11),Date varchar(30))"
create_table_sql_2="create table ${TABLENAME2} (ID int(11),Date varchar(30))"
mysql -h${DB_HOST}  -P${PORT}  -u${DB_USER} -p${DB_PASSWD} -D${DB_NAME} -e "${create_table_sql_1};${create_table_sql_2}"

#insert data
i=1000
TIME="22:22:22"
insert_sql="insert into ${TABLENAME1} values(\"$i\",\"$TIME\");"
mysql -h${DB_HOST}  -P${PORT}  -u${DB_USER} -p${DB_PASSWD} -D${DB_NAME} -e "$insert_sql"

标签:Linux, Shell, Mysql

评论已关闭