全网首例!MySQL8 MGR多主一键搭建脚本分享

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用系列 2核4GB
简介: 全网首例!MySQL8 MGR多主一键搭建脚本分享

一、概念普及

组复制可以在两种模式下运行。

  1. 1.在单主模式下,组复制具有自动选主功能,每次只有一个server成员接受更新。
  2. 2.在多主模式下,所有的 server 成员都可以同时接受更新。

今日分享MySQL8.0.33 MGR多主一键搭建脚本。单主也适用,只需要改两个参数,将my.cnf参数文件中的以下参数注释即可。

  1. #loose-group_replication_single_primary_mode = FALSE
  2. #loose-group_replication_enforce_update_everywhere_checks = TRUE

二、准备好安装包和相关脚本

赋予mysql_mgr_install.sh脚本执行权限,只需要执行mysql_mgr_install.sh即可。

  1. [root@node1 ~]# chmod +x mysql_mgr_install.sh
  2. [root@node1 ~]# sh -x mysql_mgr_install.sh

三、部分过程截图

image.svg

image.svg

image.svg

image.svg

四、脚本内容

节点1脚本

  1. cat mysql_mgr_install.sh
  2. #!/bin/bash
  3. echo "-----------------------------开始MYSQL MGR搭建--------------------------------------"
  4. start_time=$(date +%s)
  5. setenforce 0
  6. sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config
  7. #关闭防火墙
  8. systemctl stop firewalld
  9. systemctl disable firewalld
  10. systemctl status firewalld
  11. #配置yum源
  12. cd /etc/yum.repos.d/
  13. rm -rf ./*
  14. cat >> /etc/yum.repos.d/centos.repo <<-EOF
  15. [centos]
  16. name=oracle
  17. baseurl=file:///mnt
  18. enabled=1
  19. gpgcheck=0
  20. EOF
  21. cd
  22. mount /dev/sr0 /mnt
  23. yum clean all|wc -l
  24. yum makecache
  25. yum install expect*  wget*  -y
  26. # Variables
  27. HOSTS=("192.168.59.249" "192.168.59.250" "192.168.59.251")
  28. USER="root"
  29. PASSWORD="123123"
  30. SSH_DIR="/root/.ssh"
  31. # Function to generate SSH key pair on a remote host
  32. generate_ssh_key() {
  33.  local host=$1
  34.  /usr/bin/expect <<EOF
  35.    spawn ssh $USER@$host "mkdir -p $SSH_DIR && chmod 700 $SSH_DIR && \
  36.                           ssh-keygen -t rsa -b 2048 -f $SSH_DIR/id_rsa -q -N ''"
  37.    expect {
  38.      "*yes/no" { send "yes\r"; exp_continue }
  39.      "*password:" { send "$PASSWORD\r"; exp_continue }
  40.    }
  41. EOF
  42. }
  43. # Function to copy SSH public key to another host's authorized_keys
  44. copy_ssh_key() {
  45.  local src_host=$1
  46.  local dest_host=$2
  47.  local pub_key_file="/tmp/${src_host}_id_rsa.pub"
  48.  # Fetch public key from source host
  49.  /usr/bin/expect <<EOF
  50.    log_user 0
  51.    spawn ssh $USER@$src_host "cat $SSH_DIR/id_rsa.pub > $pub_key_file"
  52.    expect {
  53.      "*yes/no" { send "yes\r"; exp_continue }
  54.      "*password:" { send "$PASSWORD\r"; exp_continue }
  55.    }
  56.    log_user 1
  57. EOF

  58.  # Transfer the public key file from source host to local machine
  59.  /usr/bin/expect <<EOF
  60.    spawn scp $USER@$src_host:$pub_key_file /tmp/
  61.    expect {
  62.      "*yes/no" { send "yes\r"; exp_continue }
  63.      "*password:" { send "$PASSWORD\r"; exp_continue }
  64.    }
  65. EOF

  66.  # Append the public key to destination host's authorized_keys
  67.  pub_key=$(cat /tmp/${src_host}_id_rsa.pub)
  68.  /usr/bin/expect <<EOF
  69.    spawn ssh $USER@$dest_host "echo '$pub_key' >> $SSH_DIR/authorized_keys && chmod 600 $SSH_DIR/authorized_keys"
  70.    expect {
  71.      "*yes/no" { send "yes\r"; exp_continue }
  72.      "*password:" { send "$PASSWORD\r"; exp_continue }
  73.    }
  74. EOF

  75.  # Cleanup
  76.  rm -f /tmp/${src_host}_id_rsa.pub
  77. }
  78. # Generate SSH key pairs on all hosts
  79. for host in "${HOSTS[@]}"; do
  80.  echo "Generating SSH key on $host..."
  81.  generate_ssh_key $host
  82. done
  83. # Copy SSH keys to all hosts
  84. for src_host in "${HOSTS[@]}"; do
  85.  for dest_host in "${HOSTS[@]}"; do
  86.    if [ "$src_host" != "$dest_host" ]; then
  87.      echo "Copying SSH key from $src_host to $dest_host..."
  88.      copy_ssh_key $src_host $dest_host
  89.    fi
  90.  done
  91. done
  92. echo "SSH key distribution completed. All hosts should be able to SSH into each other without a password."
  93. hostnamectl set-hostname node1
  94. cat <<EOF >> /etc/hosts
  95. 192.168.59.249 node1
  96. 192.168.59.250 node2
  97. 192.168.59.251 node3
  98. EOF
  99. #ssh 192.168.59.250 "hostnamectl set-hostname node2"
  100. #cat <<EOF >> /etc/hosts
  101. #192.168.59.249 node1
  102. #192.168.59.240 node2
  103. #192.168.59.251 node3
  104. #EOF
  105. #ssh 192.168.59.251
  106. #hostnamectl set-hostname node2
  107. #cat <<EOF >> /etc/hosts
  108. #192.168.59.249 node1
  109. #192.168.59.240 node2
  110. #192.168.59.251 node3
  111. #EOF
  112. #ssh 192.168.59.249
  113. echo "-----------------------------开始MYSQL安装--------------------------------------"
  114. echo -e "\e[31m***************一键安装mysql任何版本数据库******************\e[0m"
  115. find / -name mysql | xargs rm -rf
  116. port=$(ss -anlp|grep mysql|wc -l)
  117. if [ $port != 0 ]
  118. then echo "mysql进程存在,请先杀掉进程"
  119. ps -ef |grep mysqld
  120. exit 1
  121. fi
  122. echo "-----------------创建所需目录及用户并上传安装包----------------------------"
  123. # 获取当前所在目录位置
  124. current_dir=$(pwd)
  125. echo "当前所在目录位置: $current_dir"
  126. # 目标路径
  127. target_dir="/opt"
  128. # 检查目标路径是否存在,如果不存在则创建
  129. if [ ! -d "$target_dir" ]; then
  130.    mkdir -p "$target_dir"
  131.    echo "已创建目录: $target_dir"
  132. fi
  133. # 移动当前目录下的所有文件到目标路径
  134. mv $current_dir/* $target_dir
  135. echo "已将当前目录下所有文件移动至 $target_dir"
  136. mkdir -p  /data/mysql
  137. groupadd mysql
  138. useradd -r -g mysql mysql
  139. cd /opt/
  140. tar -xvf mysql-8.0.33-linux-glibc2.12-x86_64.tar.xz
  141. mv mysql-8.0.33-linux-glibc2.12-x86_64/  /usr/local/
  142. cd /usr/local/
  143. mv mysql-8.0.33-linux-glibc2.12-x86_64/ mysql
  144. chown -R mysql.mysql /usr/local/mysql/
  145. echo "-----------------------------卸载原有的mysql组件--------------------------"
  146. yum list installed | grep mariadb
  147. yum -y remove mariadb*
  148. chown mysql:mysql -R /data/mysql
  149. touch /etc/my.cnf
  150. chmod 644 /etc/my.cnf
  151. MYSQL_ROOT_PASSWORD=123456
  152. cat <<EOF >/etc/my.cnf
  153. [mysqld]
  154. user=mysql
  155. basedir=/usr/local/mysql
  156. datadir=/data/mysql
  157. socket=/tmp/mysql.sock
  158. log-error=/data/mysql/mysql.err
  159. pid-file=/data/mysql/mysql.pid
  160. character-set-server=utf8mb4
  161. innodb_rollback_on_timeout = ON
  162. collation-server=utf8_general_ci
  163. sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
  164. lower_case_table_names=1
  165. max_connections=10000
  166. sync_binlog=1
  167. binlog_format=row
  168. ########basic settings########
  169. server-id =249
  170. character_set_server=utf8mb4
  171. max_allowed_packet = 16M
  172. lower_case_table_names=1
  173. slow_query_log=1
  174. slow_query_log_file=/data/mysql/slow.log
  175. ########replication settings########
  176. #####replication 复制配置###############
  177. log-bin = /data/mysql/mysql-bin
  178. max_binlog_size=500M
  179. binlog_format = row
  180. sync_binlog=1
  181. expire_logs_days=7
  182. ###group replication###########
  183. gtid_mode=on
  184. enforce_gtid_consistency= ON
  185. master_info_repository = TABLE
  186. relay_log_info_repository = TABLE
  187. binlog_checksum = NONE
  188. log_slave_updates = ON
  189. #log_slave_updates是将从服务器从主服务器收到的更新记入到从服务器自己的二进制日志文件中。
  190. transaction_write_set_extraction = XXHASH64
  191. ##server必须为每个事物收集写集合,使用XXHASH64哈希算法将其编码为散列
  192. loose-group_replication_group_name ='51837954-2d8a-11ed-bc2d-000c29f511b3'
  193. #组的名字可以随便起,但不能用主机的GTID
  194. loose-group_replication_start_on_boot = off  # #插件在server启动时不自动启动组复制
  195. loose-group_replication_bootstrap_group = off #同上
  196. loose-group_replication_ip_whitelist="192.168.59.249,192.168.59.250,192.168.59.251"
  197. report_host=192.168.59.249
  198. report_port=3306
  199. loose-group_replication_local_address = '192.168.59.249:33061'
  200. loose-group_replication_group_seeds ='192.168.59.249:33061,192.168.59.250:33061,192.168.59.251:33061'
  201. loose-group_replication_single_primary_mode = FALSE #关闭单主模式的参数
  202. loose-group_replication_enforce_update_everywhere_checks = TRUE #开启多主模式的参数
  203. ########innodb settings########
  204. innodb_flush_log_at_trx_commit = 1
  205. innodb_buffer_pool_size=128M
  206. sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
  207. secure_file_priv="/tmp"
  208. [mysql]
  209. socket=/tmp/mysql.sock
  210. default-character-set=utf8
  211. [client]
  212. EOF
  213. echo "-----------------------------------初始化数据库-----------------------------------"
  214. cd /usr/local/mysql/bin
  215. ./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql --initialize
  216. cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
  217. path=$(grep 'basedir' /etc/profile|wc -l)
  218. if [ $path !=  0 ]
  219.  then
  220.    echo -e "\e[31m MYSQL_HOME路径存在\e[0m"
  221.  else
  222.    echo "export basedir=/usr/local/mysql/bin" >> /etc/profile
  223.    echo "export PATH=\$PATH:\$basedir"        >> /etc/profile
  224.    source /etc/profile
  225. fi
  226. echo "---------------------------------启动MYSQL服务---------------------------------------"
  227. service mysql start
  228. echo 'export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib'>>/etc/profile
  229. sleep 3
  230. source /etc/profile
  231. cat /data/mysql/mysql.err|grep password
  232. chkconfig --add mysql
  233. chkconfig mysql on
  234. chkconfig --list mysql
  235. echo "-----------------------------恭喜!MYSQL安装成功--------------------------------------"
  236. end_time=$(date +%s)
  237. execution_time=$((end_time - start_time))
  238. echo "脚本执行时间:${execution_time} 秒"
  239. MYSQL_OLDPASSWORD=`awk '/A temporary password/{print $NF}' /data/mysql/mysql.err`
  240. mysqladmin  -uroot -p${MYSQL_OLDPASSWORD} password ${MYSQL_ROOT_PASSWORD}
  241. mysql -uroot -p123456 -e "update mysql.user set host ='%' where user ='root';flush privileges;"
  242. mysql -uroot -p123456 -e "SET SQL_LOG_BIN=0;"
  243. mysql -uroot -p123456 -e "create user repl@'%' identified with mysql_native_password by 'repl';"
  244. mysql -uroot -p123456 -e "GRANT REPLICATION SLAVE ON *.* TO repl@'%';"
  245. mysql -uroot -p123456 -e "GRANT CONNECTION_ADMIN ON *.* TO repl@'%';"
  246. mysql -uroot -p123456 -e "GRANT BACKUP_ADMIN ON *.* TO repl@'%';"
  247. mysql -uroot -p123456 -e "FLUSH PRIVILEGES;"
  248. mysql -uroot -p123456 -e "SET SQL_LOG_BIN=1;"
  249. mysql -uroot -p123456 -e "show plugins;"
  250. mysql -uroot -p123456 -e "CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='repl' FOR CHANNEL 'group_replication_recovery';INSTALL PLUGIN group_replication SONAME 'group_replication.so';"
  251. mysql -uroot -p123456 -e "set  global group_replication_bootstrap_group=ON;set global group_replication_ip_whitelist='192.168.59.0/24';reset master;START GROUP_REPLICATION;SELECT * FROM performance_schema.replication_group_members;SET GLOBAL group_replication_bootstrap_group=OFF;"
  252. mysql -uroot -p123456 << EOF
  253. exit
  254. EOF
  255. sleep 10
  256. ---------------开始安装MGR节点2-----------------------------------------
  257. scp /opt/mysql-8.0.33-linux-glibc2.12-x86_64.tar.xz root@192.168.59.250:/opt
  258. scp /opt/mysql_mgr_2_install.sh root@192.168.59.250:/opt/
  259. ssh -tt -p 22 root@192.168.59.250 "source /opt/mysql_mgr_2_install.sh"
  260. ln -fs /usr/local/mysql/bin/mysql /usr/bin/
  261. REMOTE_HOST="192.168.59.250"
  262. ssh =$REMOTE_HOST <<EOF
  263. if [ $? -eq 0 ]; then
  264.    echo "MySQL 安装已成功完成."
  265. else
  266.    echo "MySQL 安装失败,请检查日志或手动排查问题."
  267. fi
  268. # 添加其他命令,如清理临时文件或重启服务
  269. # 例如:rm /tmp/temp_file
  270. exit
  271. EOF
  272. sleep 10
  273. /usr/bin/expect <<EOF
  274. set timeout 60
  275. # 启动SSH命令
  276. spawn ssh root@192.168.59.249
  277. # 处理首次连接提示的 "yes/no" 和密码输入
  278. expect {
  279.    "Are you sure you want to continue connecting (yes/no)?" { send "yes\r"; exp_continue }
  280.    "password:" { send "123123\r"; exp_continue }
  281.    eof
  282. }
  283. EOF
  284. sleep 10
  285. ---------------开始安装MGR节点3-----------------------------------------
  286. scp /opt/mysql-8.0.33-linux-glibc2.12-x86_64.tar.xz root@192.168.59.251:/opt
  287. scp /opt/mysql_mgr_3_install.sh root@192.168.59.251:/opt/
  288. ssh -tt -p 22 root@192.168.59.251 "source /opt/mysql_mgr_3_install.sh"
  289. ln -fs /usr/local/mysql/bin/mysql /usr/bin/
  290. REMOTE_HOST="192.168.59.251"
  291. ssh =$REMOTE_HOST <<EOF
  292. if [ $? -eq 0 ]; then
  293.    echo "MySQL 安装已成功完成."
  294. else
  295.    echo "MySQL 安装失败,请检查日志或手动排查问题."
  296. fi
  297. # 添加其他命令,如清理临时文件或重启服务
  298. # 例如:rm /tmp/temp_file
  299. exit
  300. EOF
  301. sleep 10
  302. /usr/bin/expect <<EOF
  303. set timeout 60
  304. # 启动SSH命令
  305. spawn ssh root@192.168.59.249
  306. # 处理首次连接提示的 "yes/no" 和密码输入
  307. expect {
  308.    "Are you sure you want to continue connecting (yes/no)?" { send "yes\r"; exp_continue }
  309.    "password:" { send "123123\r"; exp_continue }
  310.    eof
  311. }
  312. EOF
  313. sleep 10
  314. mysql -uroot -p123456 -e "select * from performance_schema.replication_group_members; "
  315. mysql -uroot -p123456 << EOF
  316. exit
  317. EOF
  318. ---------------恭喜完成MYSQL MGR多主搭建-----------------------------------------

节点2脚本

  1. cat mysql_mgr_2_install.sh
  2. #!/bin/bash
  3. echo "-----------------------------开始MYSQL节点2 安装--------------------------------------"
  4. start_time=$(date +%s)
  5. setenforce 0
  6. sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config
  7. #关闭防火墙
  8. systemctl stop firewalld
  9. systemctl disable firewalld
  10. systemctl status firewalld
  11. #配置yum源
  12. cd /etc/yum.repos.d/
  13. rm -rf ./*
  14. cat >> /etc/yum.repos.d/centos.repo <<-EOF
  15. [centos]
  16. name=oracle
  17. baseurl=file:///mnt
  18. enabled=1
  19. gpgcheck=0
  20. EOF
  21. cd
  22. mount /dev/sr0 /mnt
  23. yum clean all|wc -l
  24. yum makecache
  25. yum install expect*  wget*  -y
  26. hostnamectl set-hostname node2
  27. cat <<EOF >> /etc/hosts
  28. 192.168.59.249 node1
  29. 192.168.59.250 node2
  30. 192.168.59.251 node3
  31. EOF
  32. echo "-----------------------------开始MYSQL安装--------------------------------------"
  33. echo -e "\e[31m***************一键安装mysql任何版本数据库******************\e[0m"
  34. find / -name mysql | xargs rm -rf
  35. port=$(ss -anlp|grep mysql|wc -l)
  36. if [ $port != 0 ]
  37. then echo "mysql进程存在,请先杀掉进程"
  38. ps -ef |grep mysqld
  39. exit 1
  40. fi
  41. echo "-----------------创建所需目录及用户并上传安装包----------------------------"
  42. # 获取当前所在目录位置
  43. current_dir=$(pwd)
  44. echo "当前所在目录位置: $current_dir"
  45. # 目标路径
  46. target_dir="/opt"
  47. # 检查目标路径是否存在,如果不存在则创建
  48. if [ ! -d "$target_dir" ]; then
  49.    mkdir -p "$target_dir"
  50.    echo "已创建目录: $target_dir"
  51. fi
  52. # 移动当前目录下的所有文件到目标路径
  53. mv $current_dir/* $target_dir
  54. echo "已将当前目录下所有文件移动至 $target_dir"
  55. mkdir -p  /data/mysql
  56. groupadd mysql
  57. useradd -r -g mysql mysql
  58. cd /opt/
  59. tar -xvf mysql-8.0.33-linux-glibc2.12-x86_64.tar.xz
  60. mv mysql-8.0.33-linux-glibc2.12-x86_64/  /usr/local/
  61. cd /usr/local/
  62. mv mysql-8.0.33-linux-glibc2.12-x86_64/ mysql
  63. chown -R mysql.mysql /usr/local/mysql/
  64. echo "-----------------------------卸载原有的mysql组件--------------------------"
  65. yum list installed | grep mariadb
  66. yum -y remove mariadb*
  67. chown mysql:mysql -R /data/mysql
  68. touch /etc/my.cnf
  69. chmod 644 /etc/my.cnf
  70. MYSQL_ROOT_PASSWORD=123456
  71. cat <<EOF >/etc/my.cnf
  72. [mysqld]
  73. user=mysql
  74. basedir=/usr/local/mysql
  75. datadir=/data/mysql
  76. socket=/tmp/mysql.sock
  77. log-error=/data/mysql/mysql.err
  78. pid-file=/data/mysql/mysql.pid
  79. character-set-server=utf8mb4
  80. innodb_rollback_on_timeout = ON
  81. collation-server=utf8_general_ci
  82. sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
  83. lower_case_table_names=1
  84. max_connections=10000
  85. sync_binlog=1
  86. binlog_format=row
  87. ########basic settings########
  88. server-id =250
  89. character_set_server=utf8mb4
  90. max_allowed_packet = 16M
  91. lower_case_table_names=1
  92. slow_query_log=1
  93. slow_query_log_file=/data/mysql/slow.log
  94. ########replication settings########
  95. #####replication 复制配置###############
  96. log-bin = /data/mysql/mysql-bin
  97. max_binlog_size=500M
  98. binlog_format = row
  99. sync_binlog=1
  100. expire_logs_days=7
  101. ###group replication###########
  102. gtid_mode=on
  103. enforce_gtid_consistency= ON
  104. master_info_repository = TABLE
  105. relay_log_info_repository = TABLE
  106. binlog_checksum = NONE
  107. log_slave_updates = ON
  108. #log_slave_updates是将从服务器从主服务器收到的更新记入到从服务器自己的二进制日志文件中。
  109. transaction_write_set_extraction = XXHASH64
  110. ##server必须为每个事物收集写集合,使用XXHASH64哈希算法将其编码为散列
  111. loose-group_replication_group_name ='51837954-2d8a-11ed-bc2d-000c29f511b3'
  112. #组的名字可以随便起,但不能用主机的GTID
  113. loose-group_replication_start_on_boot = off  # #插件在server启动时不自动启动组复制
  114. loose-group_replication_bootstrap_group = off #同上
  115. loose-group_replication_ip_whitelist="192.168.59.249,192.168.59.250,192.168.59.251"
  116. report_host=192.168.59.250
  117. report_port=3306
  118. loose-group_replication_local_address = '192.168.59.250:33061'
  119. loose-group_replication_group_seeds ='192.168.59.249:33061,192.168.59.250:33061,192.168.59.251:33061'
  120. loose-group_replication_single_primary_mode = FALSE #关闭单主模式的参数
  121. loose-group_replication_enforce_update_everywhere_checks = TRUE #开启多主模式的参数
  122. ########innodb settings########
  123. innodb_flush_log_at_trx_commit = 1 #改为1 是为了更安全, 值为2是性能
  124. innodb_buffer_pool_size=128M
  125. sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
  126. secure_file_priv="/tmp"
  127. [mysql]
  128. socket=/tmp/mysql.sock
  129. default-character-set=utf8
  130. [client]
  131. EOF
  132. echo "-----------------------------------初始化数据库-----------------------------------"
  133. cd /usr/local/mysql/bin
  134. ./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql --initialize
  135. cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
  136. path=$(grep 'basedir' /etc/profile|wc -l)
  137. if [ $path !=  0 ]
  138.  then
  139.    echo -e "\e[31m MYSQL_HOME路径存在\e[0m"
  140.  else
  141.    echo "export basedir=/usr/local/mysql/bin" >> /etc/profile
  142.    echo "export PATH=\$PATH:\$basedir"        >> /etc/profile
  143.    source /etc/profile
  144. fi
  145. echo "---------------------------------启动MYSQL服务---------------------------------------"
  146. service mysql start
  147. echo 'export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib'>>/etc/profile
  148. sleep 3
  149. source /etc/profile
  150. cat /data/mysql/mysql.err|grep password
  151. chkconfig --add mysql
  152. chkconfig mysql on
  153. chkconfig --list mysql
  154. echo "-----------------------------恭喜!MYSQL安装成功--------------------------------------"
  155. end_time=$(date +%s)
  156. execution_time=$((end_time - start_time))
  157. echo "脚本执行时间:${execution_time} 秒"
  158. MYSQL_OLDPASSWORD=`awk '/A temporary password/{print $NF}' /data/mysql/mysql.err`
  159. mysqladmin  -uroot -p${MYSQL_OLDPASSWORD} password ${MYSQL_ROOT_PASSWORD}
  160. mysql -uroot -p123456 -e "update mysql.user set host ='%' where user ='root';flush privileges;"
  161. mysql -uroot -p123456 -e "SET SQL_LOG_BIN=0;"
  162. mysql -uroot -p123456 -e "create user repl@'%' identified with mysql_native_password by 'repl';"
  163. mysql -uroot -p123456 -e "GRANT REPLICATION SLAVE ON *.* TO repl@'%';"
  164. mysql -uroot -p123456 -e "GRANT CONNECTION_ADMIN ON *.* TO repl@'%';"
  165. mysql -uroot -p123456 -e "GRANT BACKUP_ADMIN ON *.* TO repl@'%';"
  166. mysql -uroot -p123456 -e "FLUSH PRIVILEGES;"
  167. mysql -uroot -p123456 -e "SET SQL_LOG_BIN=1;"
  168. mysql -uroot -p123456 -e "show plugins;"
  169. mysql -uroot -p123456 -e "CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='repl' FOR CHANNEL 'group_replication_recovery';INSTALL PLUGIN group_replication SONAME 'group_replication.so';"
  170. mysql -uroot -p123456 -e "reset master;START GROUP_REPLICATION;SELECT * FROM performance_schema.replication_group_members;"
  171. mysql -uroot -p123456 << EOF
  172. exit
  173. EOF
  174. sleep 10

节点3脚本

  1. cat mysql_mgr_3_install.sh
  2. #!/bin/bash
  3. echo "-----------------------------开始MYSQL节点3安装--------------------------------------"
  4. start_time=$(date +%s)
  5. setenforce 0
  6. sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config
  7. #关闭防火墙
  8. systemctl stop firewalld
  9. systemctl disable firewalld
  10. systemctl status firewalld
  11. #配置yum源
  12. cd /etc/yum.repos.d/
  13. rm -rf ./*
  14. cat >> /etc/yum.repos.d/centos.repo <<-EOF
  15. [centos]
  16. name=oracle
  17. baseurl=file:///mnt
  18. enabled=1
  19. gpgcheck=0
  20. EOF
  21. cd
  22. mount /dev/sr0 /mnt
  23. yum clean all|wc -l
  24. yum makecache
  25. yum install expect*  wget*  -y
  26. hostnamectl set-hostname node3
  27. cat <<EOF >> /etc/hosts
  28. 192.168.59.249 node1
  29. 192.168.59.250 node2
  30. 192.168.59.251 node3
  31. EOF
  32. echo "-----------------------------开始MYSQL安装--------------------------------------"
  33. echo -e "\e[31m***************一键安装mysql任何版本数据库******************\e[0m"
  34. find / -name mysql | xargs rm -rf
  35. port=$(ss -anlp|grep mysql|wc -l)
  36. if [ $port != 0 ]
  37. then echo "mysql进程存在,请先杀掉进程"
  38. ps -ef |grep mysqld
  39. exit 1
  40. fi
  41. echo "-----------------创建所需目录及用户并上传安装包----------------------------"
  42. # 获取当前所在目录位置
  43. current_dir=$(pwd)
  44. echo "当前所在目录位置: $current_dir"
  45. # 目标路径
  46. target_dir="/opt"
  47. # 检查目标路径是否存在,如果不存在则创建
  48. if [ ! -d "$target_dir" ]; then
  49.    mkdir -p "$target_dir"
  50.    echo "已创建目录: $target_dir"
  51. fi
  52. # 移动当前目录下的所有文件到目标路径
  53. mv $current_dir/* $target_dir
  54. echo "已将当前目录下所有文件移动至 $target_dir"
  55. mkdir -p  /data/mysql
  56. groupadd mysql
  57. useradd -r -g mysql mysql
  58. cd /opt/
  59. tar -xvf mysql-8.0.33-linux-glibc2.12-x86_64.tar.xz
  60. mv mysql-8.0.33-linux-glibc2.12-x86_64/  /usr/local/
  61. cd /usr/local/
  62. mv mysql-8.0.33-linux-glibc2.12-x86_64/ mysql
  63. chown -R mysql.mysql /usr/local/mysql/
  64. echo "-----------------------------卸载原有的mysql组件--------------------------"
  65. yum list installed | grep mariadb
  66. yum -y remove mariadb*
  67. chown mysql:mysql -R /data/mysql
  68. touch /etc/my.cnf
  69. chmod 644 /etc/my.cnf
  70. MYSQL_ROOT_PASSWORD=123456
  71. cat <<EOF >/etc/my.cnf
  72. [mysqld]
  73. user=mysql
  74. basedir=/usr/local/mysql
  75. datadir=/data/mysql
  76. socket=/tmp/mysql.sock
  77. log-error=/data/mysql/mysql.err
  78. pid-file=/data/mysql/mysql.pid
  79. character-set-server=utf8mb4
  80. innodb_rollback_on_timeout = ON
  81. collation-server=utf8_general_ci
  82. sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
  83. lower_case_table_names=1
  84. max_connections=10000
  85. sync_binlog=1
  86. binlog_format=row
  87. ########basic settings########
  88. server-id =251
  89. character_set_server=utf8
  90. max_allowed_packet = 16M
  91. lower_case_table_names=1
  92. slow_query_log=1
  93. slow_query_log_file=/data/mysql/slow.log
  94. ########replication settings########
  95. #####replication 复制配置###############
  96. log-bin = /data/mysql/mysql-bin
  97. max_binlog_size=500M
  98. binlog_format = row
  99. sync_binlog=1
  100. expire_logs_days=7
  101. ###group replication###########
  102. gtid_mode=on
  103. enforce_gtid_consistency= ON
  104. master_info_repository = TABLE
  105. relay_log_info_repository = TABLE
  106. binlog_checksum = NONE
  107. log_slave_updates = ON
  108. #log_slave_updates是将从服务器从主服务器收到的更新记入到从服务器自己的二进制日志文件中。
  109. transaction_write_set_extraction = XXHASH64
  110. ##server必须为每个事物收集写集合,使用XXHASH64哈希算法将其编码为散列
  111. loose-group_replication_group_name ='51837954-2d8a-11ed-bc2d-000c29f511b3'
  112. #组的名字可以随便起,但不能用主机的GTID
  113. loose-group_replication_start_on_boot = off  # #插件在server启动时不自动启动组复制
  114. loose-group_replication_bootstrap_group = off #同上
  115. loose-group_replication_ip_whitelist="192.168.59.249,192.168.59.250,192.168.59.251"
  116. report_host=192.168.59.251
  117. report_port=3306
  118. loose-group_replication_local_address = '192.168.59.251:33061'
  119. loose-group_replication_group_seeds ='192.168.59.249:33061,192.168.59.250:33061,192.168.59.251:33061'
  120. loose-group_replication_single_primary_mode = FALSE #关闭单主模式的参数
  121. loose-group_replication_enforce_update_everywhere_checks = TRUE #开启多主模式的参数
  122. ########innodb settings########
  123. innodb_flush_log_at_trx_commit = 1 #改为1 是为了更安全, 值为2是性能
  124. innodb_buffer_pool_size=128M
  125. sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
  126. secure_file_priv="/tmp"
  127. [mysql]
  128. socket=/tmp/mysql.sock
  129. default-character-set=utf8
  130. [client]
  131. EOF
  132. echo "-----------------------------------初始化数据库-----------------------------------"
  133. cd /usr/local/mysql/bin
  134. ./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql --initialize
  135. cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
  136. path=$(grep 'basedir' /etc/profile|wc -l)
  137. if [ $path !=  0 ]
  138.  then
  139.    echo -e "\e[31m MYSQL_HOME路径存在\e[0m"
  140.  else
  141.    echo "export basedir=/usr/local/mysql/bin" >> /etc/profile
  142.    echo "export PATH=\$PATH:\$basedir"        >> /etc/profile
  143.    source /etc/profile
  144. fi
  145. echo "---------------------------------启动MYSQL服务---------------------------------------"
  146. service mysql start
  147. echo 'export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib'>>/etc/profile
  148. sleep 3
  149. source /etc/profile
  150. cat /data/mysql/mysql.err|grep password
  151. chkconfig --add mysql
  152. chkconfig mysql on
  153. chkconfig --list mysql
  154. echo "-----------------------------恭喜!MYSQL安装成功--------------------------------------"
  155. end_time=$(date +%s)
  156. execution_time=$((end_time - start_time))
  157. echo "脚本执行时间:${execution_time} 秒"
  158. MYSQL_OLDPASSWORD=`awk '/A temporary password/{print $NF}' /data/mysql/mysql.err`
  159. mysqladmin  -uroot -p${MYSQL_OLDPASSWORD} password ${MYSQL_ROOT_PASSWORD}
  160. mysql -uroot -p123456 -e "update mysql.user set host ='%' where user ='root';flush privileges;"
  161. mysql -uroot -p123456 -e "SET SQL_LOG_BIN=0;"
  162. mysql -uroot -p123456 -e "create user repl@'%' identified with mysql_native_password by 'repl';"
  163. mysql -uroot -p123456 -e "GRANT REPLICATION SLAVE ON *.* TO repl@'%';"
  164. mysql -uroot -p123456 -e "GRANT CONNECTION_ADMIN ON *.* TO repl@'%';"
  165. mysql -uroot -p123456 -e "GRANT BACKUP_ADMIN ON *.* TO repl@'%';"
  166. mysql -uroot -p123456 -e "FLUSH PRIVILEGES;"
  167. mysql -uroot -p123456 -e "SET SQL_LOG_BIN=1;"
  168. mysql -uroot -p123456 -e "show plugins;"
  169. mysql -uroot -p123456 -e "CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='repl' FOR CHANNEL 'group_replication_recovery';INSTALL PLUGIN group_replication SONAME 'group_replication.so';"
  170. mysql -uroot -p123456 -e "reset master;START GROUP_REPLICATION;SELECT * FROM performance_schema.replication_group_members;"
  171. mysql -uroot -p123456 << EOF
  172. exit
  173. EOF
  174. sleep 10

四、说明

节点1的虚拟机必须挂载镜像。本次测试操作系统为centos7。其他操作系统请根据实际情况去测试。

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
3月前
|
SQL 监控 关系型数据库
MySQL优化: CPU高 处理脚本 pt-kill脚本
MySQL优化: CPU高 处理脚本 pt-kill脚本
|
5月前
|
关系型数据库 MySQL 数据库
数据迁移脚本优化过程:从 MySQL 到 Django 模型表
在大规模的数据迁移过程中,性能问题往往是开发者面临的主要挑战之一。本文将分析一个数据迁移脚本的优化过程,展示如何从 MySQL 数据库迁移数据到 Django 模型表,并探讨优化前后的性能差异。
|
1月前
|
SQL 关系型数据库 MySQL
|
25天前
|
SQL 关系型数据库 MySQL
mysql编写sql脚本:要求表没有主键,但是想查询没有相同值的时候才进行插入
mysql编写sql脚本:要求表没有主键,但是想查询没有相同值的时候才进行插入
31 0
|
2月前
|
SQL 关系型数据库 MySQL
MySQL数据库中给表添加字段并设置备注的脚本编写
通过上述步骤,你可以在MySQL数据库中给表成功添加新字段并为其设置备注。这样的操作对于保持数据库结构的清晰和最新非常重要,同时也帮助团队成员理解数据模型的变化和字段的具体含义。在实际操作中,记得调整脚本以适应具体的数据库和表名称,以及字段的详细规范。
63 8
|
4月前
|
缓存 运维 关系型数据库
数据库容灾 | MySQL MGR与阿里云PolarDB-X Paxos的深度对比
经过深入的技术剖析与性能对比,PolarDB-X DN凭借其自研的X-Paxos协议和一系列优化设计,在性能、正确性、可用性及资源开销等方面展现出对MySQL MGR的多项优势,但MGR在MySQL生态体系内也占据重要地位,但需要考虑备库宕机抖动、跨机房容灾性能波动、稳定性等各种情况,因此如果想用好MGR,必须配备专业的技术和运维团队的支持。 在面对大规模、高并发、高可用性需求时,PolarDB-X存储引擎以其独特的技术优势和优异的性能表现,相比于MGR在开箱即用的场景下,PolarDB-X基于DN的集中式(标准版)在功能和性能都做到了很好的平衡,成为了极具竞争力的数据库解决方案。
|
3月前
|
存储 关系型数据库 MySQL
实战!MySQL主从复制一键搭建脚本分享
实战!MySQL主从复制一键搭建脚本分享
70 2
|
3月前
|
关系型数据库 MySQL Shell
MySQL数据库一键安装脚本,适合任何版本
MySQL数据库一键安装脚本,适合任何版本
89 2
|
3月前
|
关系型数据库 MySQL Shell
MySQL回滚脚本: 误操作delete binlog回滚shell脚本
MySQL回滚脚本: 误操作delete binlog回滚shell脚本
|
3月前
|
存储 关系型数据库 MySQL
[mysql]MGR简介与部署
[mysql]MGR简介与部署
下一篇
无影云桌面