mysql shell create databases

#!/bin/bash  mysql

HOSTNAME="192.168.1.224"                                           #數據庫Server信息
PORT="3306"
USERNAME="root"
PASSWORD="123456"
sql

 

DBNAME="test_db_name"                                              #要建立的數據庫的庫名稱
TABLENAME="test_table_name"                                  #要建立的數據庫的表的名稱
數據庫

 

MYSQL_CMD="mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD}"
echo ${MYSQL_CMD}
bash

 

echo "create database ${DBNAME}"spa

create_db_sql="create database IF NOT EXISTS ${DBNAME}"
echo ${create_db_sql}  | ${MYSQL_CMD}                         #建立數據庫                   
if [ $? -ne 0 ]                                                                                #判斷是否建立成功
then
 echo "create databases ${DBNAME} failed ..."
 exit 1
fi
it


echo "create table ${TABLENAME}"table

create_table_sql="create table ${TABLENAME}(
name char(6) NOT NULL,
id int default 0
)ENGINE=MyISAM DEFAULT CHARSET=latin1"
echo ${create_table_sql} | ${MYSQL_CMD} ${DBNAME}              #在給定的DB上,建立表
if [ $? -ne 0 ]                                                                                                #判斷是否建立成功
then
 echo "create  table ${DBNAME}.${TABLENAME}  fail ..."
fi
test

相關文章
相關標籤/搜索