【Mysql】shell运行mysql的sql语句
shell本身是一种脚本语言,所以不能像
java一样通过api去连接数据库。shell还是要借助mysql本身的一些运行脚本才能去执行sql语句。说到这很明白了,首先必须在机器上安装mysql。
可以通过mysql/bin/mysql这个脚本来运行sql语句,格式是mysql -hhost -Pport -uusername -ppassword database -e"sql",下面是shell的代码
[plain]
#!/bin/sh bin=/usr/local/mysql/bin
username=root password=
mysql=$bin/mysql hostname=localhost
port=3306 database=loongdisk table=userio
if [ "$password"== ""]; then
inputpwd="" else
inputpwd=-p$password fi
#year head=$database.${table}_month
echo $head
sql="alter table $head add column login int(10) unsigned not null default 0"
$mysql -h$hostname -P$port -u$username $inputpwd $database-e"$sql"