我想编写一个 shell 脚本,我想通过仅在 shell 脚本中提供用户 ID 和密码来连接到 MySQL 数据库服务器,然后运行我的 drop 并创建数据库查询。服务器将位于不同的盒子中,例如魔方。任何帮助将不胜感激。
最佳答案
首先你需要一个mysql客户端,没有的话请安装
apt-get install mysql-client
那么你必须这样写:
#!/bin/bash
USER='username'
PASS='password'
PORT=3160
HOST='hostname'
DBASE='mysqlDbName'
Query='SELECT * FROM table WHERE some=other LIMIT 10'
mysql -u$USER -p$PASS -P$PORT -h$HOST -D$DBASE <<EOF
$Query
EOF
// or you simple write the MYSQL code like
mysql -u$USER -p$PASS -P$PORT -h$HOST -D$DBASE <<EOF
USE mysql
SHOW tables
SELECT * FROM table WHERE some=other LIMIT 10
DROP table
EOF
// or in shell script and save your result to a file
#!/bin/bash
mysql -u$USER -p$PASS -h$HOST -A \
--default-character-set=utf8 \
-e $Query > /home/user/result.txt
关于mysql - 通过shell脚本连接mysql数据库服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58948094/