mysql -hhostname -Pport -uusername-ppassword -e 相關(guān)mysql的sql語句,不用在mysql的提示符下運行mysql,即可以在shell中操作mysql的方法。
#!/bin/bash
HOSTNAME="192.168.111.84"; #數(shù)據(jù)庫信息
PORT="3306";
USERNAME="root";
PASSWORD="";
DBNAME="test_db_test"; #數(shù)據(jù)庫名稱
TABLENAME="test_table_test"; #數(shù)據(jù)庫中表的名稱
#創(chuàng)建數(shù)據(jù)庫
create_db_sql="create database IF NOT EXISTS${DBNAME}"
mysql-h${HOSTNAME} -P${PORT} -u${USERNAME}-p${PASSWORD} -e "${create_db_sql}"
#創(chuàng)建表
create_table_sql="create table IF NOT EXISTS${TABLENAME} ( namevarchar(20), id int(11) default 0 )"
mysql-h${HOSTNAME} -P${PORT} -u${USERNAME}-p${PASSWORD} ${DBNAME} -e"${create_table_sql}"
#插入數(shù)據(jù)
insert_sql="insertinto ${TABLENAME} values('billchen',2)"
mysql-h${HOSTNAME} -P${PORT} -u${USERNAME}-p${PASSWORD} ${DBNAME} -e "${insert_sql}"
#查詢
select_sql="select* from ${TABLENAME}"
mysql-h${HOSTNAME} -P${PORT} -u${USERNAME}-p${PASSWORD} ${DBNAME} -e "${select_sql}"
#更新數(shù)據(jù)
update_sql="update${TABLENAME} set id=3"
mysql-h${HOSTNAME} -P${PORT} -u${USERNAME}-p${PASSWORD} ${DBNAME} -e "${update_sql}"
mysql-h${HOSTNAME} -P${PORT} -u${USERNAME}-p${PASSWORD} ${DBNAME} -e "${select_sql}"
#刪除數(shù)據(jù)
delete_sql="deletefrom ${TABLENAME}"
mysql-h${HOSTNAME} -P${PORT} -u${USERNAME}-p${PASSWORD} ${DBNAME} -e "${delete_sql}"
mysql-h${HOSTNAME} -P${PORT} -u${USERNAME}-p${PASSWORD} ${DBNAME} -e "${select_sql}"
本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點擊舉報。