简体   繁体   中英

Installing MySQL on CentOS 6 VM (Vagrant) with bash script

I'm using this script to install and create database, and edit users and permissions:

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

Then when I run the VM and try to access the database, I cannot access without any user, it sais root password and newuser password doesn't fit. I save both password to a file after the script finished. Any help here to know where is the problem with the mysql ??

Some times, when I destroy the VM and try again, the result comes with an error Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (38)

I'm running on Ubuntu 13 and the VM is CentOS 6

The problems was with:

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

The tmp file the script was creating has some errors for the MySQL syntax. If I write the tmp file or execute the commands inside the mysql console, MySQL run perfectly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM