mysql多實例應用,親測直接執行腳本可以使用,可快速部署多實例環境mysql
#!/bin/bash set -e #定義mysql_multi多實例數據的配置,如需增長,在後面函數對應地方須要增長 mydir=/data/mysql_multi #mysql sock my1=mysql3307 my2=mysql3308 #mysql_multi name mycnf1=mysqld3307 mycnf2=mysqld3308 #mysql port my1_port=3307 my2_port=3308 #須要安裝機器的內網IP nei_eth=192.168.0.131 #mysql size pool_size=2048M #######################分割線,以上內容須要按需修改###################### pack="mysql-5.6.35.tar.gz" pack_dir="mysql-5.6.35" conf_file="/usr/local/mysql" if [ ! -f ${pack} ] then echo "沒有找到mysql安裝包" exit 1 fi if [ -e /usr/local/mysql* ] || ps -ef | /bin/grep -Ev "$(basename $0)|grep" | /bin/grep -q mysql then echo "本機可能已安裝有mysql,請檢查確認!" exit 2 fi if /bin/grep mysql /etc/passwd || /bin/grep mysql /etc/group then echo "已存在mysql帳戶或用戶組" else groupadd mysql useradd -r -s /sbin/nologin -g mysql mysql fi yum remove mysql* yum -y install bison-devel ncurses-devel autoconf tar xf ${pack} \ && cd ${pack_dir} \ && echo "configuring..." \ && cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci >> /opt/make_mysql.txt \ && echo "making..." \ && make -j `grep processor /proc/cpuinfo | wc -l` >> /opt/make_mysql.txt \ && echo "installing..." \ && make install >> /opt/make_mysql.txt if [ "$?" == 0 ];then echo "程序安裝成功,即將配置並啓動" else echo "mysql安裝失敗" exit 1 fi rm -f /opt/make_mysql.txt chown -R mysql:mysql ${conf_file} cd ${conf_file} scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql [ -f /etc/my.cnf ] && mv /etc/my.cnf /etc/my.cnf.bak cp support-files/mysql.server /etc/init.d/mysql echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile source /etc/profile systemctl enable mysql systemctl start mysql && echo "OK mysql啓動成功" || echo "lost mysql啓動失敗" #配置mysql_multi函數 install_mysql_multi() { mkdir -p $mydir/{$my1,$my2}/{mydata,log} chown -R mysql:mysql $mydir/{$my1,$my2} cd ${conf_file} scripts/mysql_install_db --basedir=${conf_file} --datadir=$mydir/$my1/mydata --user=mysql scripts/mysql_install_db --basedir=${conf_file} --datadir=$mydir/$my2/mydata --user=mysql touch $mydir/{$my1,$my2}/log/log-err.err chown -R mysql:mysql $mydir/{$my1,$my2}/log/log-err.err cat >${conf_file}/my.cnf<<EOF [client] [mysqld] [mysqld_multi] user=mysql password=mysql mysqld = ${conf_file}/bin/mysqld_safe mysqladmin = ${conf_file}/bin/mysqladmin [$mycnf1] socket = /tmp/$my1.sock port = ${my1_port} user=mysql pid-file = $mydir/$my1/mysqld.pid datadir = $mydir/$my1/mydata log-bin= $mydir/$my1/log/binlog server-id = ${my1_port}001 innodb_data_home_dir = $mydir/$my1/mydata #innodb_data_file_path = ibdata1:12M;ibdata2:100M:autoextend:max:500M innodb_buffer_pool_size = ${pool_size} log_error = $mydir/$my1/log/log-err expire_logs_days = 5 bind-address = ${nei_eth} skip-name-resolve [$mycnf2] socket = /tmp/$my2.sock port = ${my2_port} user=mysql pid-file = $mydir/$my2/mysqld.pid datadir = $mydir/$my2/mydata log-bin= $mydir/$my2/log/binlog server-id = ${my2_port}001 innodb_data_home_dir = $mydir/$my2/mydata #innodb_data_file_path = ibdata1:12M;ibdata2:100M:autoextend:max:500M innodb_buffer_pool_size = ${pool_size} log_error = $mydir/$my2/log/log-err expire_logs_days = 5 bind-address = ${nei_eth} skip-name-resolve EOF #啓動各個實例 ${conf_file}/bin/mysqld_multi --defaults-file=${conf_file}/my.cnf start ${my1_port},${my2_port} #受權數據庫用戶 source /etc/profile mysql -S /tmp/${my1}.sock <<EOF SET PASSWORD = PASSWORD('123456'); GRANT ALL ON *.* TO 'htdb'@"%" IDENTIFIED BY '123456'; GRANT ALL ON *.* TO 'htdb'@"localhost" IDENTIFIED BY '123456'; flush privileges; EOF mysql -S /tmp/${my2}.sock <<EOF SET PASSWORD = PASSWORD('123456'); GRANT ALL ON *.* TO 'htdb'@"%" IDENTIFIED BY '123456'; GRANT ALL ON *.* TO 'htdb'@"localhost" IDENTIFIED BY '123456'; flush privileges; EOF } #調用函數 install_mysql_multi
PS:sql
若是mysql啓動正常,多實例啓動不了,看下err日誌報錯應該是innodb_data_file_path設置的大小有關,看下mydata下ibdata1的值,而後設置上去應該就行了;數據庫
若是是測試環境,能夠直接去掉innodb_data_home_dir和innodb_data_file_path;bash
編了很久終於編譯好了這個多實例的腳本,大部分狀況下能夠直接使用的;socket
需求不一樣須要修改一下里面的參數!函數