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

 

上一篇:SQL笔记


下一篇:mysql删除后ID不连续解决办法