2019年2月24日日曜日

VagrantでMySQL8のレプリケーション設定をした仮想マシン(CentOS、master/slaveの2台)を構築する

以下のVagrantfileでMySQL8のレプリケーション設定をした仮想マシンを構築します。mysqlnode1がマスターで、mysqlnode2がスレーブになります。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.define "mysqlnode1" do |server|
    server.vm.box = "bento/centos-7.6"
    server.vm.hostname = "mysqlnode1"
    server.vm.network :public_network, ip:"192.168.1.103"
    server.vm.network "private_network", ip: "192.168.55.103", :netmask => "255.255.255.0"
    server.vm.provider :virtualbox do |vbox|
      vbox.name = "mysqlnode1"
      vbox.cpus = 1
      vbox.memory = 1024
      vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
    end
    server.vm.provision "shell", inline: <<-SHELL
cat << EOF > /etc/hosts
192.168.55.103       mysqlnode1
192.168.55.104       mysqlnode2
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
EOF

# install mysql8
rpm -ivh https://dev.mysql.com/get/mysql80-community-release-el7-2.noarch.rpm
yum -y install mysql-community-server
cat << EOF >> /etc/my.cnf
skip_ssl
binlog_checksum=NONE
enforce_gtid_consistency=ON
gtid_mode=ON
server_id=1
default-authentication-plugin=mysql_native_password
log-bin=mysql-bin
binlog_expire_logs_seconds=2592000
EOF

systemctl start mysqld.service
systemctl enable mysqld.service

export MYSQL_PWD=`cat /var/log/mysqld.log | awk '/temporary password/ {print $NF}'`
mysql -uroot -p$MYSQL_PWD --connect-expired-password -e " ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Mysql2019#'; FLUSH PRIVILEGES; UNINSTALL COMPONENT 'file://component_validate_password'; ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';"
mysql -uroot -proot -e "CREATE USER 'root'@'%' identified by 'root'; GRANT ALL ON *.* to 'root'@'%' with grant option; FLUSH PRIVILEGES;"
mysql -uroot -proot -e "CREATE USER 'repl'@'%' identified by 'repl'; GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%'; FLUSH PRIVILEGES;"
mysql -uroot -proot -e "RESET MASTER;"

SHELL
  end
  #----------------------------------------------------------------------------------------
  config.vm.define "mysqlnode2" do |server|
    server.vm.box = "bento/centos-7.6"
    server.vm.hostname = "mysqlnode2"
    server.vm.network :public_network, ip:"192.168.1.104"
    server.vm.network "private_network", ip: "192.168.55.104", :netmask => "255.255.255.0"
    server.vm.provider :virtualbox do |vbox|
      vbox.name = "mysqlnode2"
      vbox.cpus = 2
      vbox.memory = 1024
      vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
    end
    server.vm.provision "shell", inline: <<-SHELL
cat << EOF > /etc/hosts
192.168.55.103       mysqlnode1
192.168.55.104       mysqlnode2
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
EOF

# install mysql8
rpm -ivh https://dev.mysql.com/get/mysql80-community-release-el7-2.noarch.rpm
yum -y install mysql-community-server
cat << EOF >> /etc/my.cnf
skip_ssl
binlog_checksum=NONE
enforce_gtid_consistency=ON
gtid_mode=ON
server_id=2
default-authentication-plugin=mysql_native_password
log-bin=mysql-bin
binlog_expire_logs_seconds=2592000
EOF

systemctl start mysqld.service
systemctl enable mysqld.service

export MYSQL_PWD=`cat /var/log/mysqld.log | awk '/temporary password/ {print $NF}'`
mysql -uroot -p$MYSQL_PWD --connect-expired-password -e " ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Mysql2019#'; FLUSH PRIVILEGES; UNINSTALL COMPONENT 'file://component_validate_password'; ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';"
mysql -uroot -proot -e "CREATE USER 'root'@'%' identified by 'root'; GRANT ALL ON *.* to 'root'@'%' with grant option; FLUSH PRIVILEGES;"
mysql -uroot -proot -e "RESET SLAVE;"
mysql -uroot -proot -e "CHANGE MASTER TO MASTER_HOST='mysqlnode1', MASTER_PORT=3306, MASTER_USER='repl', MASTER_PASSWORD='repl', MASTER_AUTO_POSITION=1; START SLAVE;"

SHELL
  end

end

0 件のコメント:

コメントを投稿