0

我正在使用这个脚本来安装和创建数据库,并编辑用户和权限:

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

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

有时,当我销毁 VM 并重试时,结果会出现错误 Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (38)

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

4

1 回答 1

1

问题在于:

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

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

于 2013-11-29T09:44:29.050 回答