在文章上一篇文章中
http://www.javashuo.com/article/p-sdqzpqqy-ga.html
我使用的是手動使用mysql_multi方式安裝mysql5.7.28多實例,安裝過程比較耗時,這裏我編寫了shell腳原本自動化安裝,請你們參考,腳本經測試沒有出現問題,腳本我有二次修改裏面的實例名與變量名,可能存在不一致,請自行對比確認下。腳本中有什麼能夠優化的還請多多指點,謝謝!!如下是腳本中的幾點說明:
一、mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz包上傳路徑: /opt
若是沒有此軟件包,請將此腳本中以下代碼行去掉 # 號註釋,自動聯網下載(下載過程很慢,建議提早下載並上傳到/opt路徑下)mysql
wget -c https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
二、mysql數據庫腳本上傳路徑: /opt 包含以下文件(須要修改sql文件中的實例名)linux
testone.sql testtwo.sql
四、根據服務器磁盤分配狀況,建立對應軟連接,例如:c++
mkdir /home/data ln -s /home/data/ /data
五、修改(確保局域網內值惟一) sql
mysql3306.cnf 的 server_id = 3306 mysql3307.cnf 的 server_id = 3307
六、注意修改預設的root密碼與apps應用鏈接密碼shell
Multi_PWD="Multi.db" dbone_appspwd="testone3306" dbtwo_appspwd="testtwo3307"
七、修改testone.sql數據庫實例 testone 用戶名 testone 及對應的密碼 testone3306數據庫
create database testone default character set utf8 collate utf8_bin; grant select,insert,update,delete,create,execute on testone.* to 'testone'@'%' identified by 'testone3306';
八、修改testone.sql導入數據庫時的數據庫實例名 testonevim
$MYSQL_CMD_DIR/mysql -uroot -p"${Multi_PWD}" -S ${DbOneSockfile} --comments testone < testone.sql
bash
九、修改testtwo.sql數據庫實例 testtwo 用戶名 testtwo 及對應的密碼 testtwo3307服務器
create database testtwo default character set utf8 collate utf8_bin; grant select,insert,update,delete,create,execute on testtwo.* to 'testtwo'@'%' identified by 'testtwo3307';
十、修改testtwo.sql導入數據庫時的數據庫實例名 testtwoapp
$MYSQL_CMD_DIR/mysql -uroot -p"${Multi_PWD}" -S ${DbTwoSockfile} --comments testtwo < testtwo.sql
#!/bin/bash ################################################################################################ # Install software -- Install Mysql 5.7 on CentOS 7 # Author:Danrtsey # mail:mydefiniteaim@126.com # History: 2020/01/15 Asa release ################################################################################################ [ -f /etc/init.d/functions ]&& . /etc/init.d/functions ###Check if user is root if [ $UID -ne 0 ]; then echo "Error: This script must be executed as root." exit 1 fi echo "################################################################################################" echo " 自動安裝 MySQL 5.7 on Redhat/CentOS Linux " echo "################################################################################################" #set mysql root password echo "######################### 預設 Mysql root用戶密碼 ###############################" Multi_PWD="Multi.db" #set mysql apps password echo "######################### 預設 Mysql apps用戶密碼 ###############################" dbone_appspwd="testone3306" dbtwo_appspwd="testtwo3307" echo "################################ 定義目錄路徑 #######################################" ##define mysql directory configuration variable DbOneDatadir=/data/mysql/dbone3306/data DbOneBinlogdir=/data/mysql/dbone3306/binlog DbOneLogdir=/data/mysql/dbone3306/logs DbOneSockfile=/data/mysql/dbone3306/mysql3306.sock DbOnepidfile=/data/mysql/dbone3306/mysqld3306.pid DbTwoDatadir=/data/mysql/dbtwo3307/data DbTwoBinlogdir=/data/mysql/dbtwo3307/binlog DbTwoLogdir=/data/mysql/dbtwo3307/logs DbTwoSockfile=/data/mysql/dbtwo3307/mysql3307.sock DbTwopidfile=/data/mysql/dbtwo3307/mysqld3307.pid BaseDir=/usr/local/mysql MYSQL_CMD_DIR=$BaseDir/bin Mycnf=/etc/my.cnf Dbdir=/data Softwaredir=/opt mkdir -pv /data/mysql/{dbone3306,dbtwo3307} mkdir -v /data/mysql/dbone3306/{logs,data,binlog} mkdir -v /data/mysql/dbtwo3307/{logs,data,binlog} echo "################################################################################################" echo " 修改系統參數 " echo "################################################################################################" ###set the ip in hosts hostsset() { echo "############################ Ip&Hosts Configuration #######################################" hostname=`hostname` ip=`ip a|grep 'inet '|grep -v '127.0.0.1'|awk '{print $2}'|awk -F '/' '{print $1}'` for i in ${ip} do a=`grep "${i}" /etc/hosts` if [ ! -n "${a}" ];then echo "${i} ${hostname}" >> /etc/hosts else break fi done } ntp() { yum -y install ntp >/dev/null 2>&1 systemctl enable ntpd echo 'server ntp1.aliyun.com' >> /etc/ntp.conf echo 'server ntp2.aliyun.com' >> /etc/ntp.conf systemctl start ntpd if [ $? != 0 ]; then errorExit 'ntp 啓動未成功' exit 2 fi return 0 } syspro() { sed -i 's/SELINUX=enforcing/SELINUX=permissive/g' /etc/selinux/config setenforce 0 ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime echo 'LANG="en_US.UTF-8"' >> /etc/profile && source /etc/profile cat >>/etc/security/limits.conf<<EOF * soft nproc 65535 * hard nproc 65535 * soft nofile 65535 * hard nofile 65535 EOF cat >> /etc/sysctl.conf<<EOF net.core.somaxconn = 65535 net.core.netdev_max_backlog = 65535 net.ipv4.tcp_max_syn_backlog = 65535 net.ipv4.tcp_fin_timeout = 10 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_keepalive_time = 120 net.ipv4.tcp_keepalive_intvl = 30 net.ipv4.tcp_keepalive_probes = 3 vm.swappiness = 0 net.ipv4.ip_forward=1 net.bridge.bridge-nf-call-iptables=1 net.ipv4.neigh.default.gc_thresh1=4096 net.ipv4.neigh.default.gc_thresh2=6144 net.ipv4.neigh.default.gc_thresh3=8192 kernel.shmmax = 25769803774 kernel.shmmni = 4096 kernel.shmall = 16777216 kernel.sem = 1010 129280 1010 128 net.ipv4.ip_local_port_range = 9000 65500 net.core.rmem_default = 4194304 net.core.rmem_max = 4194304 net.core.wmem_default = 262144 net.core.wmem_max = 1048576 fs.aio-max-nr = 1048576 fs.file-max = 6815744 EOF modprobe br_netfilter sysctl -p } fwport() { firewall-cmd --permanent --zone=public --add-port=3306/tcp firewall-cmd --permanent --zone=public --add-port=3307/tcp firewall-cmd --reload } clean() { echo "################################ 刪除舊Mysql、Maria #######################################" rpm -qa|grep mysql rpm -qa|grep mariadb yum -y remove mysql* >/dev/null 2>&1 yum -y remove mariadb* >/dev/null 2>&1 if [ $? -eq 0 ];then echo -e " \033[32m mariadb remove success!! \033[0m" else echo -e "\e[31;47;5m mariadb remove failed!! \e[0m" exit 3 fi #Backup old my.cnf if [ -s /etc/my.cnf ]; then mv /etc/my.cnf /etc/my.cnf.`date +%Y%m%d%H%M%S`.bak fi } # 添加用戶和組 addusers() { grep mysql /etc/passwd RETVAL=$? if [ $RETVAL -ne 0 ];then groupadd mysql useradd mysql -g mysql -s /sbin/nologin -M action "mysql user added successfully" /bin/true else action " $(echo -e " mysql user already exists ")" /bin/true exit 4 fi cat >> /etc/profile<<EOF if [ $USER = "mysql" ]; then if [ $SHELL = "/bin/ksh" ]; then ulimit -p 16384 ulimit -n 65536 else ulimit -u 16384 -n 65536 fi fi EOF source /etc/profile } #install mysql-5.7.28 dbinstall() { echo "################################ 開始下載安裝 #######################################" yum -y install bzr zlib-devel gcc-c++ ncurses ncurses-devel libev make cmake gcc autoconf automake zlib libxml libgcrypt libtool bison perl perl-devel libaio libaio-devel perl-Time-HiRes perl-DBD-MySQL perl-Digest-MD5 rsync perl-Data-Dumper net-tools wget vim openssl openssl-devel >/dev/null 2>&1 if [ $? -eq 0 ];then echo -e " \033[32m yum install success!! \033[0m" else echo -e "\e[31;47;5m yum install failed!! \e[0m" exit 5 fi cd $Softwaredir #wget -c https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz tar xf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz if [ $? -eq 0 ];then echo -e " \033[32m tar.gz decompression success!! \033[0m" else echo -e "\e[31;47;5m tar.gz decompression failed!! \e[0m" exit 6 fi mv mysql-5.7.28-linux-glibc2.12-x86_64 $BaseDir chown -R mysql:mysql $BaseDir chown -R mysql:mysql $Dbdir } #set my.cnf configfile setmycnf() { echo "################################ 配置 my.cnf #######################################" cat >$Mycnf<<EOF [mysqld_multi] mysqld = ${BaseDir}/bin/mysqld mysqladmin = ${BaseDir}/bin/mysqladmin log = /data/mysql/mysqld_multi.log #user=root #pass= [mysql] prompt="\u@jsshapp \R:\m:\s [\d]> " no-auto-rehash [mysqld3306] user = mysql port = 3306 symbolic-links = 0 #basedir = /usr/ datadir = ${DbOneDatadir} socket = ${DbOneSockfile} pid-file = ${DbOnepidfile} server_id = 3306 character_set_server = utf8 max_connections = 1000 skip_name_resolve = 1 open_files_limit = 65536 thread_cache_size = 64 table_open_cache = 4096 table_definition_cache = 1024 table_open_cache_instances = 64 max_prepared_stmt_count = 1048576 explicit_defaults_for_timestamp = true log_timestamps = system binlog_format = row log_bin = ${DbOneBinlogdir}/mysql-bin binlog_rows_query_log_events = on expire_logs_days = 7 binlog_cache_size = 4M max_binlog_cache_size = 2G max_binlog_size = 1G sync_binlog = 1 log_bin_trust_function_creators = 1 slow_query_log = on slow_query_log_file = ${DbOneDatadir}/slow.log log-error = ${DbOneLogdir}/error.log log_queries_not_using_indexes = on long_query_time = 1.000000 gtid_mode = on enforce_gtid_consistency = on default_storage_engine = innodb default_tmp_storage_engine = innodb innodb_data_file_path = ibdata1:12M:autoextend:max:2000M innodb_temp_data_file_path = ibtmp1:12M:autoextend:max:2000M innodb_buffer_pool_filename = ib_buffer_pool innodb_log_files_in_group = 3 innodb_log_file_size = 512M innodb_online_alter_log_max_size = 1024M innodb_open_files = 4096 innodb_page_size = 16k innodb_thread_concurrency = 0 innodb_read_io_threads = 4 innodb_write_io_threads = 4 innodb_purge_threads = 4 innodb_page_cleaners = 4 innodb_print_all_deadlocks = on innodb_lock_wait_timeout = 20 innodb_spin_wait_delay = 128 innodb_autoinc_lock_mode = 2 innodb_io_capacity = 200 innodb_io_capacity_max = 2000 #innodb_flush_neighbors = innodb_log_buffer_size = 8M innodb_flush_log_at_timeout = 1 innodb_flush_log_at_trx_commit = 2 innodb_buffer_pool_size = 1024M innodb_buffer_pool_instances = 4 autocommit = 1 innodb_buffer_pool_dump_pct = 25 innodb_buffer_pool_dump_at_shutdown = ON innodb_buffer_pool_load_at_startup = ON [mysqld3307] user = mysql port = 3307 symbolic-links = 0 lower_case_table_names = 1 #basedir = /usr/ datadir = ${DbTwoDatadir} socket = ${DbTwoSockfile} pid-file = ${DbTwopidfile} server_id = 3307 character_set_server = utf8 max_connections = 1000 skip_name_resolve = 1 open_files_limit = 65536 thread_cache_size = 64 table_open_cache = 4096 table_definition_cache = 1024 table_open_cache_instances = 64 max_prepared_stmt_count = 1048576 explicit_defaults_for_timestamp = true log_timestamps = system binlog_format = row log_bin = ${DbTwoBinlogdir}/mysql-bin binlog_rows_query_log_events = on expire_logs_days = 7 binlog_cache_size = 4M max_binlog_cache_size = 2G max_binlog_size = 1G sync_binlog = 1 slow_query_log = on slow_query_log_file = ${DbTwoDatadir}/slow.log log-error = ${DbTwoLogdir}/error.log log_queries_not_using_indexes = on long_query_time = 1.000000 gtid_mode = on enforce_gtid_consistency = on default_storage_engine = innodb default_tmp_storage_engine = innodb innodb_data_file_path = ibdata1:12M:autoextend:max:2000M innodb_temp_data_file_path = ibtmp1:12M:autoextend:max:2000M innodb_buffer_pool_filename = ib_buffer_pool innodb_log_files_in_group = 3 innodb_log_file_size = 512M innodb_online_alter_log_max_size = 1024M innodb_open_files = 4096 innodb_page_size = 16k innodb_thread_concurrency = 0 innodb_read_io_threads = 4 innodb_write_io_threads = 4 innodb_purge_threads = 4 innodb_page_cleaners = 4 innodb_print_all_deadlocks = on innodb_lock_wait_timeout = 20 innodb_spin_wait_delay = 128 innodb_autoinc_lock_mode = 2 innodb_io_capacity = 200 innodb_io_capacity_max = 2000 #innodb_flush_neighbors = innodb_log_buffer_size = 8M innodb_flush_log_at_timeout = 1 innodb_flush_log_at_trx_commit = 2 innodb_buffer_pool_size = 1024M innodb_buffer_pool_instances = 4 autocommit = 1 innodb_buffer_pool_dump_pct = 25 innodb_buffer_pool_dump_at_shutdown = ON innodb_buffer_pool_load_at_startup = ON [mysqldump] quick max_allowed_packet = 32M EOF } #dbone initialize dboneinitialize() { echo "################################ dbone 初始化 #######################################" $MYSQL_CMD_DIR/mysqld --defaults-file=$Mycnf --initialize --basedir=$BaseDir --user=mysql --datadir=$DbOneDatadir > $DbOneLogdir/error.log 2>&1 if [ $? -eq 0 ];then echo -e " \033[32m dbone initialize success!! \033[0m" else echo -e "\e[31;47;5m dbone initialize failed!! \e[0m" exit 7 fi less $DbOneLogdir/error.log|grep 'A temporary password is generated for root@localhost:' A=$? count=0 while [ $count -lt 10 ] do if [ $A -eq 0 ];then echo -e " \033[32m mysql initialize success!! \033[0m" break else echo -e "\e[31;47;5m the result is null,check again!! \e[0m" count=$[${count}+1] fi done #create ssl_rsa echo "################################ dbone 生成ssl受權 #######################################" $MYSQL_CMD_DIR/mysql_ssl_rsa_setup --user=mysql --basedir=$BaseDir --datadir=$DbOneDatadir if [ $? -eq 0 ];then echo -e " \033[32m create ssl_rsa success!! \033[0m" else echo -e "\e[31;47;5m create ssl_rsa failed!! \e[0m" exit 8 fi } #dbtwo initialize dbtwoinitialize() { echo "################################ dbtwo 初始化 #######################################" $MYSQL_CMD_DIR/mysqld --defaults-file=$Mycnf --initialize --basedir=$BaseDir --user=mysql --datadir=$DbTwoDatadir > $DbTwoLogdir/error.log 2>&1 if [ $? -eq 0 ];then echo -e " \033[32m dbtwo initialize success!! \033[0m" else echo -e "\e[31;47;5m dbtwo initialize failed!! \e[0m" exit 9 fi less $DbTwoLogdir/error.log|grep 'A temporary password is generated for root@localhost:' B=$? count=0 while [ $count -lt 10 ] do if [ $B -eq 0 ];then echo -e " \033[32m mysql initialize success!! \033[0m" break else echo -e "\e[31;47;5m the result is null,check again!! \e[0m" count=$[${count}+1] fi done #create ssl_rsa echo "################################ dbtwo 生成ssl受權 #######################################" $MYSQL_CMD_DIR/mysql_ssl_rsa_setup --user=mysql --basedir=$BaseDir --datadir=$DbTwoDatadir if [ $? -eq 0 ];then echo -e " \033[32m create ssl_rsa success!! \033[0m" else echo -e "\e[31;47;5m create ssl_rsa failed!! \e[0m" exit 10 fi } # start dbone service && change tmptroot pwd dbonestart() { echo "################################ dbone 服務啓動 #######################################" cat << EOF >> /etc/profile export PATH=\$PATH:${BaseDir}/bin EOF source /etc/profile chown -R mysql:mysql $DbOneLogdir $MYSQL_CMD_DIR/mysqld_multi start 3306 if [ $? -eq 0 ];then count=0 while [ $count -lt 120 ] do cat $DbOneLogdir/error.log |grep '/data/mysql/dbone3306/mysql3306.sock' C=$? if [ $C -eq 0 ];then echo -e " \033[32m dbone start success!! \033[0m" break else count=$[${count}+1] echo -e "\e[31;47;5m 3306 is not exist check the $count time!! \e[0m" sleep 1 fi done else echo -e "\e[31;47;5m dbone use mysqld_multi start failed,please check the error.log !! \e[0m" cat $DbOneLogdir/error.log |grep ERROR exit 11 fi echo "################################ 獲取 dbone root用戶臨時密碼 #######################################" DbOnedbTempPass=$(less $DbOneLogdir/error.log|grep 'A temporary password is generated for root@localhost:' |awk '{print $NF}') echo $DbOnedbTempPass echo "################################ 自動修改 dbone root用戶初始密碼 ###############################" $MYSQL_CMD_DIR/mysqladmin -uroot -p"$DbOnedbTempPass" -S $DbOneSockfile password $Multi_PWD if [ $? -eq 0 ];then echo -e " \033[32m dbone rootpwd changed success!! \033[0m" else echo -e "\e[31;47;5m dbone rootpwd changed failed!! \e[0m" exit 12 fi echo "################################ 驗證 dbone root用戶初始密碼 ###############################" MYSQL_CMD="$MYSQL_CMD_DIR/mysql -uroot -p"${Multi_PWD}" -S ${DbOneSockfile}" $MYSQL_CMD -e "show databases;" | grep information_schema if [ $? -eq 0 ];then echo -e " \033[32m dbone rootpwd is correct!! \033[0m" else echo -e "\e[31;47;5m dbone rootpwd is wrong!! \e[0m" exit 13 fi echo "################################ 修改 my.cnf multi 啓動用戶與密碼 #######################################" sed -i "s@^#user=root@user=root@g" /etc/my.cnf sed -i "s@^#pass=@pass=${Multi_PWD}@g" /etc/my.cnf if [ $? -eq 0 ];then echo -e " \033[32m my.cnf changed success!! \033[0m" else echo -e "\e[31;47;5m my.cnf changed failed!! \e[0m" exit 14 fi echo "################################ 驗證 dbone 服務關閉是否正常 #######################################" $MYSQL_CMD_DIR/mysqld_multi stop 3306 netstat -tnlp|grep 3306 if [ $? -ne 0 ];then echo -e " \033[32m dbone stop success!! \033[0m" else echo -e "\e[31;47;5m dbone stop failed!! \e[0m" exit 15 fi } # start dbtwo service && change tmptroot pwd dbtwostart() { echo "################################ dbtwo 服務啓動 #######################################" source /etc/profile chown -R mysql:mysql $DbTwoLogdir $MYSQL_CMD_DIR/mysqld_multi start 3307 if [ $? -eq 0 ];then count=0 while [ $count -lt 120 ] do cat $DbTwoLogdir/error.log |grep '/data/mysql/dbtwo3307/mysql3307.sock' D=$? if [ $D -eq 0 ];then echo -e " \033[32m dbtwo start success!! \033[0m" break else count=$[${count}+1] echo -e "\e[31;47;5m 3307 is not exist check the $count time!! \e[0m" sleep 1 fi done else echo -e "\e[31;47;5m dbtwo use mysqld_multi start failed,please check the error.log !! \e[0m" cat $DbTwoLogdir/error.log |grep ERROR exit 16 fi echo "################################ 獲取 dbtwo root用戶臨時密碼 #######################################" DbTwodbTempPass=$(less $DbTwoLogdir/error.log|grep 'A temporary password is generated for root@localhost:' |awk '{print $NF}') echo $DbTwodbTempPass echo "################################ 自動修改 dbtwo root用戶初始密碼 ###############################" $MYSQL_CMD_DIR/mysqladmin -uroot -p"$DbTwodbTempPass" -S $DbTwoSockfile password $Multi_PWD if [ $? -eq 0 ];then echo -e " \033[32m dbtwo rootpwd changed success!! \033[0m" else echo -e "\e[31;47;5m dbtwo rootpwd changed failed!! \e[0m" exit 17 fi echo "################################ 驗證 dbtwo root用戶初始密碼 ###############################" MYSQL_CMD="$MYSQL_CMD_DIR/mysql -uroot -p"${Multi_PWD}" -S ${DbTwoSockfile}" $MYSQL_CMD -e "show databases;" | grep information_schema if [ $? -eq 0 ];then echo -e " \033[32m dbtwo rootpwd is correct!! \033[0m" else echo -e "\e[31;47;5m dbtwo rootpwd is wrong!! \e[0m" exit 18 fi echo "################################ 驗證 dbtwo 服務關閉是否正常 #######################################" $MYSQL_CMD_DIR/mysqld_multi stop 3307 netstat -tnlp|grep 3307 if [ $? -ne 0 ];then echo -e " \033[32m dbtwo stop success!! \033[0m" else echo -e "\e[31;47;5m dbtwo stop failed!! \e[0m" exit 19 fi } dboneimp() { echo "############################### 再次重啓 dbone 數據庫確認系統服務正常 ##################################" source /etc/profile cat >$DbOneLogdir/error.log<<EOF use the mysqld_multi to start the dbone again EOF chown -R mysql:mysql $DbOneLogdir $MYSQL_CMD_DIR/mysqld_multi start 3306 if [ $? -eq 0 ];then count=0 while [ $count -lt 120 ] do cat $DbOneLogdir/error.log |grep '/data/mysql/dbone3306/mysql3306.sock' E=$? if [ $E -eq 0 ];then echo -e " \033[32m dbone start success!! \033[0m" break else count=$[${count}+1] echo -e "\e[31;47;5m 3306 is not exist check the $count time!! \e[0m" sleep 1 fi done else echo -e "\e[31;47;5m dbone use mysqld_multi start failed,please check the error.log !! \e[0m" cat $DbOneLogdir/error.log |grep ERROR exit 20 fi echo "############################### 建立 testone 庫 ##################################" MYSQL_CMD="$MYSQL_CMD_DIR/mysql -uroot -p"${Multi_PWD}" -S ${DbOneSockfile}" $MYSQL_CMD -e "create database testone default character set utf8 collate utf8_bin;grant select,insert,update,delete,create,alter,execute on testone.* to 'testone'@'%' identified by 'testone3306';flush privileges;" > /tmp/dbone_create.log cat /tmp/dbone_create.log |grep "Table 'mysql.servers' doesn't exist" if [ $? -eq 0 ] then echo -e "\e[31;47;5m 建立 testone 數據庫失敗!\e[0m" exit 21 fi $MYSQL_CMD -e "show databases;" | grep testone if [ $? -eq 0 ] then echo -e " \033[32m 建立 testone 數據庫成功!! \033[0m" else echo -e "\e[31;47;5m 建立 testone 數據庫失敗!\e[0m" exit 22 fi echo "############################### testone 導入數據 ##################################" cd $Softwaredir $MYSQL_CMD_DIR/mysql -uroot -p"${Multi_PWD}" -S ${DbOneSockfile} testone < testone.sql if [ $? -eq 0 ] then echo -e " \033[32m 導入數據成功!! \033[0m" else echo -e "\e[31;47;5m 導入數據失敗!\e[0m" exit 23 fi } dbtwoimp() { echo "############################### 再次重啓 dbtwo 數據庫確認系統服務正常 ##################################" source /etc/profile cat >$DbTwoLogdir/error.log<<EOF use the mysqld_multi to start the dbtwo again EOF chown -R mysql:mysql $DbTwoLogdir $MYSQL_CMD_DIR/mysqld_multi start 3307 if [ $? -eq 0 ];then count=0 while [ $count -lt 120 ] do cat $DbTwoLogdir/error.log |grep '/data/mysql/dbtwo3307/mysql3307.sock' F=$? if [ $F -eq 0 ];then echo -e " \033[32m dbtwo start success!! \033[0m" break else count=$[${count}+1] echo -e "\e[31;47;5m 3307 is not exist check the $count time!! \e[0m" sleep 1 fi done else echo -e "\e[31;47;5m dbtwo use mysqld_multi start failed,please check the error.log !! \e[0m" cat $DbTwoLogdir/error.log |grep ERROR exit 24 fi echo "############################### 建立 testtwo 庫 ##################################" MYSQL_CMD="$MYSQL_CMD_DIR/mysql -uroot -p"${Multi_PWD}" -S ${DbTwoSockfile}" $MYSQL_CMD -e "create database testtwo default character set utf8 collate utf8_bin;grant select,insert,update,delete,create,alter,execute on testtwo.* to 'testtwo'@'%' identified by 'testtwo3307';flush privileges;" > /tmp/dbtwo_create.log cat /tmp/dbtwo_create.log |grep "Table 'mysql.servers' doesn't exist" if [ $? -eq 0 ] then echo -e "\e[31;47;5m 建立 testtwo 數據庫失敗!\e[0m" exit 25 fi $MYSQL_CMD -e "show databases;" | grep testtwo if [ $? -eq 0 ] then echo -e " \033[32m 建立 testtwo 數據庫成功!! \033[0m" else echo -e "\e[31;47;5m 建立 testtwo 數據庫失敗!\e[0m" exit 26 fi echo "############################### testtwo 導入數據 ##################################" cd $Softwaredir $MYSQL_CMD_DIR/mysql -uroot -p"${Multi_PWD}" -S ${DbTwoSockfile} testtwo < testtwo.sql if [ $? -eq 0 ] then echo -e " \033[32m 導入數據成功!! \033[0m" else echo -e "\e[31;47;5m 導入數據失敗!\e[0m" exit 27 fi } main() { hostsset ntp syspro fwport clean addusers dbinstall setmycnf dboneinitialize dbtwoinitialize dbonestart dbtwostart dboneimp dbtwoimp } main echo "####################### 安裝完成 (請記錄dbone數據庫testone信息) ##############################" echo "root密碼:" echo -e "\e[31;47;5m $Multi_PWD \e[0m" echo "數據庫實例鏈接密碼:" echo -e "\e[30;47;5m $dbone_appspwd \e[0m" echo "####################### 安裝完成 (請記錄dbtwo數據庫testtwo信息) ##############################" echo "root密碼:" echo -e "\e[31;47;5m $Multi_PWD \e[0m" echo "數據庫實例鏈接密碼:" echo -e "\e[30;47;5m $dbtwo_appspwd \e[0m" source /etc/profile