我正在尝试创建一个bash脚本,以便在MySQL 7 (x64)上自动安装CentOS CommunityServer5.7版本。
我偶然看到了这个可爱的脚本https://github.com/mysql/mysql-docker/blob/mysql-server/5.7/docker-entrypoint.sh,并将以下内容组合在一起:
#!/bin/sh
DATADIR="/var/lib/mysql"
MYSQL_ROOT_PASSWORD="$(pwmake 128)"
echo ' -> Removing previous mysql installation';
systemctl stop mysqld.service && yum remove -y mysql-community-server && rm -rf /var/lib/mysql && rm -rf /var/log/mysqld.log
echo ' -> Installing mysql database server';
yum localinstall -y https://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
yum install -y mysql-community-server
echo ' -> Creating mysql data directory'
mkdir -p "$DATADIR"
chown -R mysql:mysql "$DATADIR"
echo ' -> Initializing mysql database'
mysqld --initialize-insecure=on --user=mysql --datadir="$DATADIR"
mysqld --user=mysql --datadir="$DATADIR" --skip-networking & pid="$!"
mysql=( mysql --protocol=socket -uroot )
for i in {30..0}; do
if echo 'SELECT 1' | "${mysql[@]}" &> /dev/null; then
break
fi
echo 'MySQL init process in progress ...'
sleep 1
done
if [ "$i" = 0 ]; then
echo >&2 'MySQL init process failed'
exit 1
fi
echo ' -> Setting mysql server root password';
mysql_tzinfo_to_sql /usr/share/zoneinfo | "${mysql[@]}" mysql
"${mysql[@]}" <<-EOSQL
SET @@SESSION.SQL_LOG_BIN=0;
DELETE FROM mysql.user where user != 'mysql.sys';
CREATE USER 'root'@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;
GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION ;
DROP DATABASE IF EXISTS test ;
FLUSH PRIVILEGES ;
EOSQL
if [ ! -z "$MYSQL_ROOT_PASSWORD" ]; then
mysql+=( -p"${MYSQL_ROOT_PASSWORD}" )
fi
if ! kill -s TERM "$pid" || ! wait "$pid"; then
echo >&2 'MySQL init process failed.'
exit 1
fi
chown -R mysql:mysql "$DATADIR"
echo " -> Mysql server setup completed, your root password: $MYSQL_ROOT_PASSWORD"脚本应该执行以下操作:
当我运行脚本时,这是我得到的输出:
Total download size: 142 M
Installed size: 652 M
Downloading packages:
mysql-community-server-5.7.10-1.el7.x86_64.rpm | 142 MB 00:00:04
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : mysql-community-server-5.7.10-1.el7.x86_64 1/1
Verifying : mysql-community-server-5.7.10-1.el7.x86_64 1/1
Installed:
mysql-community-server.x86_64 0:5.7.10-1.el7
Complete!
-> Creating mysql data directory
-> Initializing mysql database
MySQL init process in progress ...
MySQL init process in progress ...
...snipped...
MySQL init process in progress ...
MySQL init process in progress ...
MySQL init process failed我认为剧本中的步骤很直接,但却失败了。知道为什么会这样吗?
发布于 2016-01-02 19:42:10
我通过重写shell脚本成功地实现了这一点。这对我来说是完美无缺的:)
#!/bin/bash
mysqlRootPass="$(pwmake 128)"
echo ' -> Removing previous mysql server installation'
systemctl stop mysqld.service && yum remove -y mysql-community-server && rm -rf /var/lib/mysql && rm -rf /var/log/mysqld.log && rm -rf /etc/my.cnf
echo ' -> Installing mysql server (community edition)'
yum localinstall -y https://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
yum install -y mysql-community-server
echo ' -> Starting mysql server (first run)'
systemctl enable mysqld.service
systemctl start mysqld.service
tempRootDBPass="`grep 'temporary.*root@localhost' /var/log/mysqld.log | tail -n 1 | sed 's/.*root@localhost: //'`"
echo ' -> Setting up new mysql server root password'
systemctl stop mysqld.service
rm -rf /var/lib/mysql/*logfile*
wget -O /etc/my.cnf "https://my-site.com/downloads/mysql/512MB.cnf"
systemctl start mysqld.service
mysqladmin -u root --password="$tempRootDBPass" password "$mysqlRootPass"
mysql -u root --password="$mysqlRootPass" -e <<-EOSQL
DELETE FROM mysql.user WHERE User='';
DROP DATABASE IF EXISTS test;
DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';
DELETE FROM mysql.user where user != 'mysql.sys';
CREATE USER 'root'@'%' IDENTIFIED BY '${mysqlRootPass}';
GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EOSQL
systemctl status mysqld.service
echo " -> MySQL server installation completed, root password: $mysqlRootPass";发布于 2016-03-06 03:28:45
echo 'First step : Disabling and removing partially MariaDB'
yum remove MariaDB-common MariaDB-compat MariaDB-server
yum repolist enabled | grep "mariadb.*"
echo 'Removing Percona'
yum remove Percona-Server-client-55 Percona-Server-server-55
Percona- Server-shared-55.i686 percona-release
echo 'Last step : Removing completely mariadb'
yum remove mariadb mariadb-server
echo 'Removing mysql'
rm /etc/my.cnf
yum remove mysql-server mysql-libs mysql-devel mysql
echo 'Installing mysql assuming the rpm file is already download'
yum localinstall mysql57-community-release-el7-7.noarch.rpm
yum repolist enabled | grep "mysql.*-community.*"
yum install mysql-community-server
echo 'Starting mysql'
systemctl enable mysqld
systemctl start mysqld
echo 'Finally the root password needed for mysql to start'
echo 'oldpass will contain the temporary password value'
echo 'newpass must be written here and must meet Mysql Password Policies'
oldpass=$( grep 'temporary.*root@localhost' /var/log/mysqld.log |
tail -n 1 | sed 's/.*root@localhost: //' )
newpass="Password!"
mysqladmin -u root --password=${oldpass} password $newpass https://stackoverflow.com/questions/34569373
复制相似问题