RPM包安装mysql,采用shell脚本实现自动安装、配置与卸载

版本是5.5.8。

 

自动安装配置步骤:

1. 先在/etc下面放一份my.cnf,这里是链接的方式。安装之前先运行卸载脚本,它会杀掉相关进程。

2. 运行install.sh进行安装与配置

 

my.cnf

# The following options will be passed to all MySQL clients
[client]
#password       = your_password
port            = 3306
socket          = /opt/alu/data/mysql/mysql.sock

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
port            = 3306
socket          = /opt/alu/data/mysql/mysql.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M

 

datadir=/opt/alu/data/mysql/datadir
# query log
log=/opt/alu/logs/3rd_party/mysql/mysql-query
# slow query log
log-slow-queries=/opt/alu/logs/3rd_party/mysql/mysql-slow-queries
# binary log
log-bin=/opt/alu/logs/3rd_party/mysql/binary_log/mysql-bin
pid-file=/opt/alu/data/mysql/mysql.pid

 

 

没有复制的部分都是原配置。这里使用的是huge那份。

 

 

 

 

install.sh

 

#!/bin/bash

# uninstall mysql
. uninstall.sh

LOG_FILE=/opt/alu/logs/3rd_party/mysql/install.log

MYSQL_DIR=/opt/alu/3rd_party/mysql/bin


# install mysql rpm
rpm -Uhv `ls ${MYSQL_DIR}/*.rpm` >> ${LOG_FILE} 2>&1

 

MYSQL_OLD=/var/lib/mysql
MYSQL_NEW=/opt/alu/data

 

cp -rp ${MYSQL_OLD} ${MYSQL_NEW}
rm -rf ${MYSQL_OLD}

MYSQL_BASE_DIR=${MYSQL_NEW}/mysql

 

if [ ! -f ${MYSQL_BASE_DIR}/datadir ]; then
    mkdir ${MYSQL_BASE_DIR}/datadir
fi

 

MYSQL_DATA_DIR=${MYSQL_BASE_DIR}/datadir

 

mv -f ${MYSQL_BASE_DIR}/test ${MYSQL_DATA_DIR}
mv -f ${MYSQL_BASE_DIR}/information_schema ${MYSQL_DATA_DIR}
mv -f ${MYSQL_BASE_DIR}/mysql ${MYSQL_DATA_DIR}
mv -f ${MYSQL_BASE_DIR}/ib* ${MYSQL_DATA_DIR}

 

 

service mysql start


NEWPWD=nbiops
HOSTNAME=`hostname`

mysql -uroot -e "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('${NEWPWD}'); /
    SET PASSWORD FOR 'root'@'127.0.0.1' = PASSWORD('${NEWPWD}'); /
    SET PASSWORD FOR 'root'@'::1' = PASSWORD('${NEWPWD}'); /
    SET PASSWORD FOR 'root'@'${HOSTNAME}' = PASSWORD('${NEWPWD}'); /
    DROP USER ''@'localhost'; /
    DROP USER ''@'${HOSTNAME}'; /
    CREATE USER 'nbiops'@'localhost' IDENTIFIED BY 'nbiops'; /
    GRANT ALL PRIVILEGES ON *.* TO 'nbiops'@'localhost' WITH GRANT OPTION; /
    CREATE USER 'nbiops'@'%' IDENTIFIED BY 'nbiops'; /
    GRANT ALL PRIVILEGES ON *.* TO 'nbiops'@'%' WITH GRANT OPTION; /
    FLUSH PRIVILEGES;"  >> ${LOG_FILE} 2>&1

echo "install done."

 

 

 

 

uninstall.sh

 

#!/bin/bash

LOG_FILE=/opt/alu/logs/3rd_party/mysql/uninstall.log

 

# stop mysql server
/etc/init.d/mysql stop >> ${LOG_FILE} 2>&1

 

#uninstall mysql RPMs
rpm -e `rpm -qa "MySQL*"`

 

PID_3306=`ps -ef | grep mysql | grep -v grep | awk '{print $2}'`
if [ ${PID_3306} ]; then
    for pid in ${PID_3306}
    do
    kill ${pid}
    done
fi

 

echo "uninstall done."

 

你可能感兴趣的:(数据库)