Bootstrap

远程操作mysql的shell脚本

远程操作mysql的shell脚本

话不多说 看代码!

创建shell脚本

touch test.sh

编辑脚本

vi test.sh

编辑内容

#!/usr/bin/expect
#连接mysql服务器
spawn ssh 服务器名称@服务器端口
send "yes\r"
#输入服务器密码
expect "*password:" { send "服务器密码*\r" }
#输入mysql连接命令
expect "$ " { send "mysql -u root -p数据库密码*\r" }
#选择数据库
send "use 数据库;\r"
#sql语句

#清除表数据
send "TRUNCATE TABLE T_BLACKLIST_USER;\r"
#按条件清除数据
send "DELETE FROM T_KEYWORD_CONFIG WHERE CHATBOT_ID NOT IN ('sip:[email protected]');\r"
#删除表
send "DROP TABLE T_RECORD;\r"

#退出mysql连接
send "quit;\r"
expect eof

按esc 输入:wq!(强制保存) wq(保存) 回车

:wq

给脚本权限 u(全部权限) x(脚本权限)

chmod u+x test.sh 
或者
chmod +x test.sh 
;