Think before you speak, read before you think.

bash创建mysql数据库脚本

需要批量创建数据库及数据库对应用户,写了一个对于mysql的执行脚本。

#!/bin/bash
 #用法:将数据库名/用户名用:分隔开,每个一行,写入一个文件,执行脚本+文件名,可创建数据库,并为其分配同名用户
 IFS=:
 while read dbname dbpassword
 do
 sql_user='root'
 sql_pass='root'
 mysql -u $sql_user -p$sql_pass -s <<EOT
 create database if not exists $dbname default charset utf8 collate utf8_general_ci;
 QUIT
 EOT
 mysql -u $sql_user -p$sql_pass -s << EOF
 insert into mysql.user (Host,User,Password) values ("localhost","$dbname",PASSWORD("$dbpassword"));
 flush privileges;
 grant all on $dbname.* to $dbname@localhost;
 flush privileges;
 QUIT
 EOF
done < $1

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *