適用於綠色版Mysql(編譯安裝後的Mysql)向其餘機器複製安裝,將腳本放在mysql目錄中執行便可安裝mysql
#!/bin/bash mysqluser=mysql mysqldata=/data/mysql mysqldir=$(cd `dirname $0`; pwd) path=$(cd `dirname $0`; pwd) #添加mysql用戶 /usr/sbin/useradd $mysqluser -s /sbin/nologin if [ ! -d $mysqldata ];then mkdir -p $mysqldata/data mkdir -p $mysqldata/logs fi chown $mysqluser:$mysqluser $mysqldata -R chown $mysqluser:$mysqluser $mysqldir -R #編輯配置文件 #cp ${path}/my.cnf /etc/my.cnf cp ${path}/support-files/my-innodb-heavy-4G.cnf /etc/my.cnf sed -i "/\[mysqld\]/ a \pid-file=${mysqldata}\/mysql.pid" /etc/my.cnf sed -i "/pid-file=.*/ a \basedir=${mysqldir}" /etc/my.cnf sed -i "/basedir=.*/ a \datadir=${mysqldata}\/data" /etc/my.cnf sed -i "/datadir=.*/ a \log-error=${mysqldata}\/logs/error.log" /etc/my.cnf cp ${path}/support-files/mysql.server /etc/init.d/mysqld #sed -i "0,/basedir=/ s:basedir=:basedir=${mysqldir}:" /etc/init.d/mysqld #sed -i "0,/basedir=/ s:datadir=:datadir=${mysqldata}\/data:" /etc/init.d/mysqld chmod +x /etc/init.d/mysqld #初始化mysql echo "開始初始化mysql。。。" ${path}/scripts/mysql_install_db --user=$mysqluser --datadir=${mysqldata}/data/ --basedir=${mysqldir} >> /dev/null 2>&1 if [ $? -eq 0 ];then echo "mysql初始化完成!!!" else echo "mysql初始化失敗!!!" exit 1 fi /etc/init.d/mysqld start
綠色版mysql配置主從同步(基本配置)sql
#!/bin/bash passwd=123456 master=192.168.31.100 slave=192.168.31.101 masterdir=$(cd `dirname $0`; pwd) slavedir=/opt/mysql path=$(cd `dirname $0`; pwd) function set_nopass() { ssh-keygen -t rsa ssh-copy-id -i /root/.ssh/id_rsa.pub root@$slave } function sync_file() { echo "開始推送安裝文件。。。" rsync -av $masterdir/ $slave:$slavedir/ >> /dev/null 2>&1 if [ $? -eq 0 ];then echo "文件推送完成!!!" else echo "文件推送失敗!!!" exit 1 fi } function install_mysql() { cd $path echo "開始在master上安裝mysql。。。" sh Install_mysql-green.sh ${path}/bin/mysqladmin -uroot password $passwd if [ $? -eq 0 ];then echo "mysql-master密碼配置完成!!!" echo "密碼: $passwd" else echo "mysql-master密碼配置失敗!!!" exit 1 fi echo "開始在slave上安裝mysql。。。" sync_file ssh $slave "cd $slavedir; sh Install_mysql-green.sh" ssh $slave "${slavedir}/bin/mysqladmin -uroot password $passwd" if [ $? -eq 0 ];then echo "mysql-slave密碼配置完成!!!" echo "密碼: $passwd" else echo "mysql-slave密碼配置失敗!!!" exit 1 fi } function config_master_slave() { echo "開始配置主從同步。。。" ssh $slave "sed -i '0,/server-id = .*/ s/server-id = .*/server-id = 20/' /etc/my.cnf" ssh $slave "/etc/init.d/mysqld restart" ${path}/bin/mysql -uroot -p$passwd -e "grant replication slave on *.* to 'replication'@'%' identified by 'replication';" num_masterifle=$(${path}/bin/mysql -uroot -p$passwd -e 'show master status;' | grep -v File|awk '{print $1}') num_masterlog=$(${path}/bin/mysql -uroot -p$passwd -e 'show master status;' | grep -v File|awk '{print $2}') ssh root@$slave >> /dev/null 2>&1 << EOF ${slavedir}/bin/mysql -uroot -p$passwd -e "change master to master_host='$master', master_user='replication', master_password='replication', master_log_file='$num_masterifle',master_log_pos=$num_masterlog;" ${slavedir}/bin/mysql -uroot -p$passwd -e "slave start;" EOF status=$(ssh $slave "${slavedir}/bin/mysql -uroot -p123456 -e 'show slave status\G'" | awk '/Slave_IO_Running:/ {print $2} /Slave_SQL_Running:/ {print $2}' | tr '\n' ' ') if [[ ${status} == 'Yes Yes ' ]];then echo "mysql主從同步配置成功!!!" else echo "mysql主從同步配置失敗!!!" fi } function main(){ set_nopass install_mysql config_master_slave } main