记录一下 CentOS 离线安装 MySQL 并配置多实列主从复制的过程, 如果有旧版 Mariadb,先卸载旧版 Mariadb。
卸载系统自带的 Mariadb 1 2 3 rpm -qa|grep mariadb rpm -e --nodeps filename rm -f /etc/my.cnf
创建 mysql 用户组 1 2 groupadd mysql useradd -g mysql mysql
下载安装包 1 wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.12-linux-glibc2.12-x86_64.tar.xz
解压文件到目录 /usr/local 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 cp mysql-8.0.12-linux-glibc2.12-x86_64.tar.xz /usr/local /mysql-8.0.12-linux-glibc2.12-x86_64.tar.xz cd /usr/local xz -d your_file_name.tar.xz tar -xvf your_file_name.tar tar xvJf your_file_name.tar.xz tar xvJf mysql-8.0.12-linux-glibc2.12-x86_64.tar.xz mv mysql-8.0.12-linux-glibc2.12-x86_64 mysql
配置 /etc/my.cnf 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 [mysqld_multi] mysqld = /usr/local /mysql/bin/mysqld_safe mysqladmin = /usr/local /mysql/bin/mysqladmin user = root [mysqld1] port =33306 datadir =/data/mysqldata/data1 socket =/var/lib/mysql/mysql1.sock pid-file =/var/lib/mysql/mysql1.pid user =mysql server_id =33306 log_bin =/data/mysqldata/data1/mysql-bin log_bin_index =/data/mysqldata/data1/mysql-bin.index expire_logs_days =10 [mysqld2] port =33307 datadir =/data/mysqldata/data2 socket =/var/lib/mysql/mysql2.sock pid-file =/var/lib/mysql/mysql2.pid user =mysql server_id =33307 log_bin =/data/mysqldata/data2/mysql-bin log_bin_index =/data/mysqldata/data2/mysql-bin.index expire_logs_days =10
初始化数据目录 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 mkdir /var/lib/mysql chown -R mysql:mysql /var/lib/mysql mkdir /data/mysqldata chown -R mysql:mysql /data/mysqldata mkdir /data/mysqldata/data1 chown -R mysql:mysql /data/mysqldata/data1 mkdir /data/mysqldata/data2 chown -R mysql:mysql /data/mysqldata/data2 cd /usr/local /mysql/bin./mysql_install_db --basedir=/usr/local /mysql --datadir=/data/mysqldata/data1 --user=mysql ./mysql_install_db --basedir=/usr/local /mysql --datadir=/data/mysqldata/data2 --user=mysql ./mysqld --initialize --basedir=/usr/local /mysql --datadir=/data/mysqldata/data1 --user=mysql --console ./mysqld --initialize --basedir=/usr/local /mysql --datadir=/data/mysqldata/data2 --user=mysql --console
启动实例 1 2 3 4 cd /usr/local /mysql/binmysqld_multi start 1 mysqld_multi start 2 mysqld_multi report
主库创建同步账号 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 ./mysql -S /var/lib/mysql/mysql1.sock -p your-password USE mysql; UPDATE user SET authentication_string = PASSWORD('new-password' ), password_expired = 'N' , password_last_changed = NOW() WHERE user = 'root' ; FLUSH PRIVILEGES; GRANT ALL PRIVILEGES ON *.* TO 'root' @'%' IDENTIFIED BY 'new-password' WITH GRANT OPTION; CREATE USER 'repl' @'%' IDENTIFIED BY 'repl-password' ; GRANT REPLICATION SLAVE ON *.* TO 'repl' @'%' ; SHOW MASTER STATUS; SHOW BINARY LOGS; SET GLOBAL EXPIRE_LOGS_DAYS = 10; FLUSH LOGS; SHOW BINARY LOGS; PURGE BINARY LOGS TO 'mysql-bin.000080' ; SHOW BINARY LOGS;
从库配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 server-id =2 relay-log =/dbdata/data/relay-log relay-log-index =/dbdata/data/relay-log.index CHANGE MASTER TO MASTER_HOST=‘master_host_name’, MASTER_PORT=port_number MASTER_USER=‘replication_user_name’, MASTER_PASSWORD=‘replication_password’, MASTER_LOG_FILE=‘recorded_log_file_name’, MASTER_LOG_POS=recorded_log_position; start slave;