使用bash脚本在CentOS 6 VM(Vagrant)上安装MySQL

时间:2013-10-16 12:25:35

标签: mysql bash ubuntu centos vagrant

我正在使用此脚本来安装和创建数据库,以及编辑用户和权限:

sudo yum install mysql-server mysql-client -y

MYSQL_NAME="databasename"
MYSQL_USER="newuser"
MYSQL_PASS="12341234"
MYSQL_ROOTPWD=`openssl rand -base64 12`
MYSQL_TMPFILE=`mktemp --suffix=.sql`

echo "UPDATE mysql.user SET Password=PASSWORD('$MYSQL_ROOTPWD') WHERE User='root';" | tee -a $MYSQL_TMPFILE
echo "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');" | tee -a $MYSQL_TMPFILE
echo "DELETE FROM mysql.user WHERE User='';" | tee -a $MYSQL_TMPFILE
echo "DROP DATABASE test;" | tee -a $MYSQL_TMPFILE
echo "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';" | tee -a $MYSQL_TMPFILE
echo "CREATE DATABASE $MYSQL_NAME CHARACTER SET 'utf8';" | tee -a $MYSQL_TMPFILE
echo "GRANT ALL ON $MYSQL_NAME.* TO '$MYSQL_USER'@'127.0.0.1' IDENTIFIED BY '$MYSQL_PASS';" | tee -a $MYSQL_TMPFILE
echo "GRANT ALL ON test_$MYSQL_NAME.* TO '$MYSQL_USER'@'127.0.0.1' IDENTIFIED BY '$MYSQL_PASS';" | tee -a $MYSQL_TMPFILE
echo "FLUSH PRIVILEGES;" | tee -a $MYSQL_TMPFILE

cat $MYSQL_TMPFILE | mysql -u root
rm $MYSQL_TMPFILE

然后当我运行VM并尝试访问数据库时,我无法访问任何用户,它的root密码和新用户密码不适合。脚本完成后,我将两个密码都保存到文件中。这里有任何帮助,知道mysql的问题在哪里?

有些时候,当我销毁虚拟机并再次尝试时,结果会出现错误无法通过套接字连接到本地MySQL服务器'/var/mysql/mysql.sock'(38)

我在Ubuntu 13上运行,而VM是CentOS 6

1 个答案:

答案 0 :(得分:1)

问题在于:

echo "FLUSH PRIVILEGES;" | tee -a $MYSQL_TMPFILE

脚本创建的tmp文件有一些MySQL语法错误。如果我编写tmp文件或在mysql控制台内执行命令,MySQL运行完美