shell本身是一种脚本语言,所以不能像java一样通过api去连接数据库。shell还是要借助mysql本身的一些运行脚本才能去执行sql语句。说到这很明白了,首先必须在机器上安装mysql。

   可以通过mysql/bin/mysql这个脚本来运行sql语句,格式是mysql -hhost -Pport -uusername -ppassword database -e"sql",下面是shell的代码

#!/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"

    其中为了解决mysql密码为空的问题还是做了一定的调整,可能解决方式不太好,如果有好的方式,可以留言~~