mysql shell脚本

mysql shell连接脚本

本地连接及远程链接

#!/bin/bash

#连接MySQL数据库
Host=127.0.0.1
User=username
PASSWORD=password
PORT=3306
DBNAME="test_db_test"
TABLENAME="test_table_test"
CREATE_DB_SQL="SELECT * FROM ${DBNAME}.${TABLENAME}" 
 
#本地连接
mysql -u$User -p$PASSWORD <<EOF
use ${DBNAME};
SELECT * FROM ${DBNAME}.${TABLENAME};
COMMIT;
EOF
if [ $? = 0 ];then
    echo "create yes"
fi

#远程连接
mysql -h$Host -u$User -p$PASSWORD <<EOF #指定Host,其他不变
use DATABASE_NAME;
SELECT * FROM TABLE_NAME;
EOF 

-e 方式连接

#!/bin/bash

#连接MySQL数据库
Host=127.0.0.1
User=username
PASSWORD=password
PORT=3306
DBNAME="test_db_test"
TABLENAME="test_table_test"
CREATE_DB_SQL="SELECT * FROM ${DBNAME}.${TABLENAME}"
mysql -h${Host}  -P${PORT}  -u${User} -p${PASSWORD}  -D ${DBNAME} -e "${CREATE_DB_SQL}"
if [ $? = 0 ];then
    echo "create yes"
fi

 

posted @ 2019-06-23 19:33  吊车尾88  阅读(1141)  评论(0编辑  收藏