linux上使用shell脚本进行数据库及thinkphp的操作
1 #!/bin/bash 2 3 HOSTNAME="192.168.111.84" #数据库信息 4 5 PORT="3306" 6 7 USERNAME="root" 8 9 PASSWORD="" 10 11 12 DBNAME="test_db_test" #数据库名称 13 14 TABLENAME="test_table_test" #数据库中表的名称 15 16 17 #创建数据库 18 19 create_db_sql="create database IF NOT EXISTS ${DBNAME}" 20 21 mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} -e "${create_db_sql}" 22 23 24 #创建表 25 26 create_table_sql="create table IF NOT EXISTS ${TABLENAME} ( name varchar(20), id int(11) default 0 )" 27 28 mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${create_table_sql}" 29 30 31 #插入数据 32 33 insert_sql="insert into ${TABLENAME} values('billchen',2)" 34 35 mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${insert_sql}" 36 37 38 #查询 39 40 select_sql="select * from ${TABLENAME}" 41 42 mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${select_sql}" 43 44 45 #更新数据 46 47 update_sql="update ${TABLENAME} set id=3" 48 49 mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${update_sql}" 50 51 mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${select_sql}" 52 53 54 #删除数据 55 56 delete_sql="delete from ${TABLENAME}" 57 58 mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${delete_sql}" 59 60 mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${select_sql}"
#也可以写 HOSTNAME="localhost",端口号 PORT可以不设定
例:
1 #!/bin/bash 2 HOSTNAME="localhost" #数据库信息 3 PORT="3306" 4 USERNAME="app_shhengyin_c" 5 PASSWORD="87JXiSJ" 6 DBNAME="app_shhengyin_c" #数据库名称 7 TABLENAME="wx_user" #数据库中表的名称 8 #更新数据,更新抽奖次数为1 9 update_sql="update ${TABLENAME} set score=1" 10 mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${update_sql}" 11 #更新数据,更新分享数为0 12 update_sql="update ${TABLENAME} set sharnum=0" 13 mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${update_sql}"
shell脚本执行thinphp模块
php /www/xxx/index.php home/index/test
实例:宝塔中通过shell执行thinkphp的命令
php /www/wwwroot/www.shhy.com/index.php app/signIn/Indexc/sendAllMessage/mid/5