Shell操作mysql數據庫 mysql -hhostname -Pport -uusername-ppassword -e 相關mysql的sql語句,不用在mysql的提示符下運行mysql,即可以在shell中操作mysql的方法。 #!/bin/bash HOSTNAME="192.168.111.84"; #數據庫信息 PORT="3306"; USERNAME="root"; PASSWORD=""; DBNAME="test_db_test"; #數據庫名稱 TABLENAME="test_table_test"; #數據庫中表的名稱 #創建數據庫 create_db_sql="create database IF NOT EXISTS${DBNAME}" mysql-h${HOSTNAME} -P${PORT} -u${USERNAME}-p${PASSWORD} -e "${create_db_sql}" #創建表 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}" #插入數據 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}" #更新數據 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}" #刪除數據 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}"