MySQL8.4一主一从环境搭建实现
作者:zxrhhm
一、MySQL数据库服务环境搭建
1.1、下载及上传mysql二进制安装包
下载地址
MySQL :: Download MySQL Community Server
上传mysql-8.4.0-linux-glibc2.17-x86_64.tar.xz到soft目录
ls -lsa /soft
1.2、增加MySQL用户组及用户
groupadd mysql useradd -r -s /bin/false -g mysql mysql mkdir -p /mysql/data/3306/data mkdir -p /mysql/backup/backup-db chown -R mysql:mysql /mysql
1.3、编辑配置文件my.cnf
vi /mysql/data/3306/my.cnf
[mysqld] server-id=573306 port=3306 basedir=/mysql/app/mysql datadir=/mysql/data/3306/data log-error=/mysql/log/3306/superdb-error.log socket=/mysql/data/3306/mysql.sock pid-file=/mysql/data/3306/mysql.pid character-set-server=utf8mb4 lower_case_table_names=1 innodb_log_file_size=1G default-storage-engine=INNODB mysql_native_password=on secure_file_priv=''
[mysql] prompt=(\\u@\\h)[\\d]>\\_ [client] port=3306 default-character-set=utf8mb4
1.4、解压
cd /soft ls xz -d mysql-8.4.0-linux-glibc2.17-x86_64.tar.xz tar xvf mysql-8.4.0-linux-glibc2.17-x86_64.tar mv mysql-8.4.0-linux-glibc2.17-x86_64 /mysql/app/mysql
1.5、mysql初始化
/mysql/app/mysql/bin/mysqld --defaults-file=/mysql/data/3306/my.cnf --initialize --user=mysql --basedir=/mysql/app/mysql --datadir=/mysql/data/3306/data
1.6、安全模式启动mysql
/mysql/app/mysql/bin/mysqld_safe --defaults-file=/mysql/data/3306/my.cnf &
1.7、设置软连接sock软连接
ln -sf /mysql/data/3306/mysql.sock /tmp/mysql.sock
1.8、编辑环境变量mysql home目录及登陆提示
vi ~/.bash_profile
PATH=$PATH:/mysql/app/mysql/bin:$HOME/bin export MYSQL_PS1="(\u@\h:\p)[\d]>"
source ~/.bash_profile
tail -fn300 /mysql/log/3306/superdb-error.log
1.9、登陆mysql设置密码
defaultmysqlpwd=`grep 'A temporary password' /mysql/log/3306/superdb-error.log |awk -F "root@localhost: " '{ print $2}' |tail -n1` mysql -uroot -p"${defaultmysqlpwd}" --connect-expired-password <<EOF ALTER USER 'root'@'localhost' IDENTIFIED BY 'Root@2024'; EOF sleep 1 mysql -uroot -p create user 'root'@'%' identified by 'Root@2024'; grant all privileges on *.* to 'root'@'%'; flush privileges; exit
1.10、设置service mysqld 服务
mv /mysql/app/mysql/support-files/mysql.server /mysql/app/mysql/support-files/mysql.server.bak
vi /mysql/app/mysql/support-files/mysql.server
#!/bin/sh # Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB # This file is public domain and comes with NO WARRANTY of any kind # MySQL daemon start/stop script. # Usually this is put in /etc/init.d (at least on machines SYSV R4 based # systems) and linked to /etc/rc3.d/S99mysql and /etc/rc0.d/K01mysql. # When this is done the mysql server will be started when the machine is # started and shut down when the systems goes down. # Comments to support chkconfig on RedHat Linux # chkconfig: 2345 64 36 # description: A very fast and reliable SQL database engine. # Comments to support LSB init script conventions ### BEGIN INIT INFO # Provides: mysql # Required-Start: $local_fs $network $remote_fs # Should-Start: ypbind nscd ldap ntpd xntpd # Required-Stop: $local_fs $network $remote_fs # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: start and stop MySQL # Description: MySQL is a very fast and reliable SQL database engine. ### END INIT INFO # If you install MySQL on some other places than /usr/local/mysql, then you # have to do one of the following things for this script to work: # # - Run this script from within the MySQL installation directory # - Create a /etc/my.cnf file with the following information: # [mysqld] # basedir=<path-to-mysql-installation-directory> # - Add the above to any other configuration file (for example ~/.my.ini) # and copy my_print_defaults to /usr/bin # - Add the path to the mysql-installation-directory to the basedir variable # below. # # If you want to affect other MySQL variables, you should make your changes # in the /etc/my.cnf, ~/.my.cnf or other MySQL configuration files. # If you change base dir, you must also change datadir. These may get # overwritten by settings in the MySQL configuration files. basedir=/mysql/app/mysql datadir=/mysql/data/3306/data # Default value, in seconds, afterwhich the script should timeout waiting # for server start. # Value here is overriden by value in my.cnf. # 0 means don't wait at all # Negative numbers mean to wait indefinitely service_startup_timeout=900 # Lock directory for RedHat / SuSE. lockdir='/var/lock/subsys' lock_file_path="$lockdir/mysql" # The following variables are only set for letting mysql.server find things. # Set some defaults mysqld_pid_file_path=/mysql/data/3306/mysql.pid if test -z "$basedir" then basedir=/mysql/app/mysql bindir=/mysql/app/mysql/bin if test -z "$datadir" then datadir=/mysql/data/3306/data fi sbindir=/mysql/app/mysql/bin libexecdir=/mysql/app/mysql/bin else bindir="$basedir/bin" if test -z "$datadir" then datadir="/mysql/app/3306/data" fi sbindir="$basedir/sbin" libexecdir="$basedir/libexec" fi # datadir_set is used to determine if datadir was set (and so should be # *not* set inside of the --basedir= handler.) datadir_set= # # Use LSB init script functions for printing messages, if possible # lsb_functions="/lib/lsb/init-functions" if test -f $lsb_functions ; then . $lsb_functions else log_success_msg() { echo " SUCCESS! $@" } log_failure_msg() { echo " ERROR! $@" } fi PATH="/sbin:/usr/sbin:/bin:/mysql/app/mysql/bin:/usr/bin:$basedir/bin" export PATH mode=$1 # start or stop [ $# -ge 1 ] && shift other_args="$*" # uncommon, but needed when called from an RPM upgrade action # Expected: "--skip-networking --skip-grant-tables" # They are not checked here, intentionally, as it is the resposibility # of the "spec" file author to give correct arguments only. case `echo "testing\c"`,`echo -n testing` in *c*,-n*) echo_n= echo_c= ;; *c*,*) echo_n=-n echo_c= ;; *) echo_n= echo_c='\c' ;; esac parse_server_arguments() { for arg do case "$arg" in --basedir=*) basedir=`echo "$arg" | sed -e 's/^[^=]*=//'` bindir="$basedir/bin" if test -z "$datadir_set"; then datadir="$basedir/data" fi sbindir="$basedir/sbin" libexecdir="$basedir/libexec" ;; --datadir=*) datadir=`echo "$arg" | sed -e 's/^[^=]*=//'` datadir_set=1 ;; --pid-file=*) mysqld_pid_file_path=`echo "$arg" | sed -e 's/^[^=]*=//'` ;; --service-startup-timeout=*) service_startup_timeout=`echo "$arg" | sed -e 's/^[^=]*=//'` ;; esac done } wait_for_pid () { verb="$1" # created | removed pid="$2" # process ID of the program operating on the pid-file pid_file_path="$3" # path to the PID file. i=0 avoid_race_condition="by checking again" while test $i -ne $service_startup_timeout ; do case "$verb" in 'created') # wait for a PID-file to pop into existence. test -s "$pid_file_path" && i='' && break ;; 'removed') # wait for this PID-file to disappear test ! -s "$pid_file_path" && i='' && break ;; *) echo "wait_for_pid () usage: wait_for_pid created|removed pid pid_file_path" exit 1 ;; esac # if server isn't running, then pid-file will never be updated if test -n "$pid"; then if kill -0 "$pid" 2>/dev/null; then : # the server still runs else # The server may have exited between the last pid-file check and now. if test -n "$avoid_race_condition"; then avoid_race_condition="" continue # Check again. fi # there's nothing that will affect the file. log_failure_msg "The server quit without updating PID file ($pid_file_path)." return 1 # not waiting any more. fi fi echo $echo_n ".$echo_c" i=`expr $i + 1` sleep 1 done if test -z "$i" ; then log_success_msg return 0 else log_failure_msg return 1 fi } # Get arguments from the my.cnf file, # the only group, which is read from now on is [mysqld] if test -x "$bindir/my_print_defaults"; then print_defaults="$bindir/my_print_defaults" else # Try to find basedir in /etc/my.cnf conf=/mysql/data/3306/my.cnf print_defaults= if test -r $conf then subpat='^[^=]*basedir[^=]*=\(.*\)$' dirs=`sed -e "/$subpat/!d" -e 's//\1/' $conf` for d in $dirs do d=`echo $d | sed -e 's/[ ]//g'` if test -x "$d/bin/my_print_defaults" then print_defaults="$d/bin/my_print_defaults" break fi done fi # Hope it's in the PATH ... but I doubt it test -z "$print_defaults" && print_defaults="my_print_defaults" fi # # Read defaults file from 'basedir'. If there is no defaults file there # check if it's in the old (depricated) place (datadir) and read it from there # extra_args="" if test -r "/mysql/data/3306/my.cnf" then extra_args="-e /mysql/data/3306/my.cnf" fi parse_server_arguments `$print_defaults $extra_args mysqld server mysql_server mysql.server` # # Set pid file if not given # if test -z "$mysqld_pid_file_path" then mysqld_pid_file_path=$datadir/`hostname`.pid else case "$mysqld_pid_file_path" in /* ) ;; * ) mysqld_pid_file_path="$datadir/$mysqld_pid_file_path" ;; esac fi case "$mode" in 'start') # Start daemon # Safeguard (relative paths, core dumps..) cd $basedir echo $echo_n "Starting MySQL" if test -x $bindir/mysqld_safe then # Give extra arguments to mysqld with the my.cnf file. This script # may be overwritten at next upgrade. $bindir/mysqld_safe --defaults-file=/mysql/data/3306/my.cnf --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null & wait_for_pid created "$!" "$mysqld_pid_file_path"; return_value=$? # Make lock for RedHat / SuSE if test -w "$lockdir" then touch "$lock_file_path" fi exit $return_value else log_failure_msg "Couldn't find MySQL server ($bindir/mysqld_safe)" fi ;; 'stop') # Stop daemon. We use a signal here to avoid having to know the # root password. if test -s "$mysqld_pid_file_path" then # signal mysqld_safe that it needs to stop touch "$mysqld_pid_file_path.shutdown" mysqld_pid=`cat "$mysqld_pid_file_path"` if (kill -0 $mysqld_pid 2>/dev/null) then echo $echo_n "Shutting down MySQL" kill $mysqld_pid # mysqld should remove the pid file when it exits, so wait for it. wait_for_pid removed "$mysqld_pid" "$mysqld_pid_file_path"; return_value=$? else log_failure_msg "MySQL server process #$mysqld_pid is not running!" rm "$mysqld_pid_file_path" fi # Delete lock for RedHat / SuSE if test -f "$lock_file_path" then rm -f "$lock_file_path" fi exit $return_value else log_failure_msg "MySQL server PID file could not be found!" fi ;; 'restart') # Stop the service and regardless of whether it was # running or not, start it again. if $0 stop $other_args; then $0 start $other_args else log_failure_msg "Failed to stop running server, so refusing to try to start." exit 1 fi ;; 'reload'|'force-reload') if test -s "$mysqld_pid_file_path" ; then read mysqld_pid < "$mysqld_pid_file_path" kill -HUP $mysqld_pid && log_success_msg "Reloading service MySQL" touch "$mysqld_pid_file_path" else log_failure_msg "MySQL PID file could not be found!" exit 1 fi ;; 'status') # First, check to see if pid file exists if test -s "$mysqld_pid_file_path" ; then read mysqld_pid < "$mysqld_pid_file_path" if kill -0 $mysqld_pid 2>/dev/null ; then log_success_msg "MySQL running ($mysqld_pid)" exit 0 else log_failure_msg "MySQL is not running, but PID file exists" exit 1 fi else # Try to find appropriate mysqld process mysqld_pid=`pidof $libexecdir/mysqld` # test if multiple pids exist pid_count=`echo $mysqld_pid | wc -w` if test $pid_count -gt 1 ; then log_failure_msg "Multiple MySQL running but PID file could not be found ($mysqld_pid)" exit 5 elif test -z $mysqld_pid ; then if test -f "$lock_file_path" ; then log_failure_msg "MySQL is not running, but lock file ($lock_file_path) exists" exit 2 fi log_failure_msg "MySQL is not running" exit 3 else log_failure_msg "MySQL is running but PID file could not be found" exit 4 fi fi ;; *) # usage basename=`basename "$0"` echo "Usage: $basename {start|stop|restart|reload|force-reload|status} [ MySQL server options ]" exit 1 ;; esac exit 0
cp /mysql/app/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod 775 /etc/init.d/mysqld
chkconfig --list chkconfig --add mysqld chkconfig --list
service mysqld status
service mysqld stop
service mysqld start
二、数据准备,模拟生产的环境
2.1、主节点创建库及测试表
create database db01; use db01; create table dept ( deptno int unsigned auto_increment primary key comment '部门编号', dname varchar(15) comment '部门名称' , loc varchar(50) comment '部门所在位置' )engine = innodb default charset=utf8mb4 comment='员工部门表'; create table emp( empno int unsigned auto_increment primary key comment '雇员编号', ename varchar(15) comment '雇员姓名' , job varchar(10) comment '雇员职位' , mgr int unsigned comment '雇员对应的领导的编号', hiredate date comment '雇员的雇佣日期' , sal decimal(7,2) comment '雇员的基本工资' , comm decimal(7,2) comment '奖金' , deptno int unsigned comment '所在部门' , foreign key(deptno) references dept(deptno) )engine = innodb default charset =utf8mb4 comment='雇员信息表'; create table salgrade ( grade int comment '工资等级', losal int comment '此等级的最低工资', hisal int comment '此等级的最高工资' )engine=innodb default charset=utf8mb4 comment='工资等级表'; alter table salgrade add constraint pk_salgrade_primary primary key (grade,losal,hisal); create table bonus ( ename varchar(10) comment '雇员姓名', job varchar(9) comment '雇员职位', sal decimal(7,2) comment '雇员工资', comm decimal(7,2) comment '雇员奖金' )engine=innodb default charset=utf8mb4 comment='雇员奖金表' ; alter table bonus add constraint pk_bonus_primary primary key (ename,job); show full columns from emp; select * from information_schema.tables where table_schema='db01' and table_name='emp'; select * from information_schema.columns where table_schema='db01' and table_name='emp'; insert into dept values (10,'ACCOUNTING','NEW YORK'); insert into dept values (20,'RESEARCH','DALLAS'); insert into dept values (30,'SALES','CHICAGO'); insert into dept values (40,'OPERATIONS','BOSTON'); insert into emp values (7369,'SMITH','CLERK',7902,'1980-12-17',800,null,20); insert into emp values (7499,'ALLEN','SALESMAN',7698,'1981-2-20',1600,300,30); insert into emp values (7521,'WARD','SALESMAN',7698,'1981-2-22',1250,500,30); insert into emp values (7566,'JONES','MANAGER',7839,'1981-4-2',2975,null,20); insert into emp values (7654,'MARTIN','SALESMAN',7698,'1981-9-28',1250,1400,30); insert into emp values (7698,'BLAKE','MANAGER',7839,'1981-5-1',2850,null,30); insert into emp values (7782,'CLARK','MANAGER',7839,'1981-6-9',2450,null,10); insert into emp values (7788,'SCOTT','ANALYST',7566,'87-7-13',3000,null,20); insert into emp values (7839,'KING','PRESIDENT',null,'1981-11-17',5000,null,10); insert into emp values (7844,'TURNER','SALESMAN',7698,'1981-9-8',1500,0,30); insert into emp values (7876,'ADAMS','CLERK',7788,'87-7-13',1100,null,20); insert into emp values (7900,'JAMES','CLERK',7698,'1981-12-3',950,null,30); insert into emp values (7902,'FORD','ANALYST',7566,'1981-12-3',3000,null,20); insert into emp values (7934,'MILLER','CLERK',7782,'1982-1-23',1300,null,10); insert into salgrade values (1,700,1200); insert into salgrade values (2,1201,1400); insert into salgrade values (3,1401,2000); insert into salgrade values (4,2001,3000); insert into salgrade values (5,3001,9999); commit;
三、搭建主从及测试
3.1、设置复制源配置
show variables like '%server_id%';
3.2、为复制创建用户
注意:主从节点都执行,方便后期主从切换
CREATE USER 'repl'@'%' identified by 'Root@3306'; GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';
测试从库连接主库
mysql -u repl -p -h 192.168.80.50
3.3、获取复制源的二进制日志位置
在主节点的不同会话中,使用 SHOW BINARY LOG STATUS语句确定当前二进制日志文件名和位置:
SHOW BINARY LOG STATUS;
(root@localhost:mysql.sock)[(none)]>SHOW BINARY LOG STATUS; +---------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+-------------------+ | binlog.000001 | 11518 | | | | +---------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
3.4、主节点备份及上传到备节点
使用mysqldump工具创建要复制的所有数据库的转储。这是推荐的方法,尤其是在使用 InnoDB.
mysqldump --all-databases --master-data > dbdump.db
如果不使用 --master-data,则需要手动锁定单独会话中的所有表 --ignore-table使用选项 排除数据库中的所有表 --databases选项仅命名要转储的那些数据库
cd /mysql/backup/backup-db mysqldump -uroot -p --all-databases --master-data=1 > dbfulldump.db
du -sh dbfulldump.db
从节点检查目录是的否存在
mkdir -p /mysql/backup/backup-db
拷贝主节点的备份到从节点
scp -r /mysql/backup/backup-db/dbfulldump.db root@192.168.80.51:/mysql/backup/backup-db
3.5、设置副本
3.5.1、调整server_id
show variables like '%server%'; +---------------------------------+--------------------------------------+ | Variable_name | Value | +---------------------------------+--------------------------------------+ | character_set_server | utf8mb4 | | collation_server | utf8mb4_0900_ai_ci | | immediate_server_version | 999999 | | innodb_dedicated_server | OFF | | innodb_ft_server_stopword_table | | | original_server_version | 999999 | | server_id | 513306 | | server_id_bits | 32 | | server_uuid | 81c80be9-10d4-11ef-b4df-000c290e14ee | +---------------------------------+--------------------------------------+ 9 rows in set (0.00 sec)
3.5.2、导入数据
show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)
source dbdump.db
3.5.3、配置连接到主服务器的相关信息
CHANGE REPLICATION SOURCE TO SOURCE_HOST='192.168.80.50', SOURCE_LOG_FILE='binlog.000001', SOURCE_LOG_POS=11518, SOURCE_PORT=3306, SOURCE_USER='repl', SOURCE_PASSWORD='Root@3306';
3.5.4、启动从服务器的复制线程
start REPLICA;
3.6、验证
show REPLICA status \G show processlist \G select * from db01.emp; select * from db01.dept;
3.7、在线启用 GTID 事务
3.7.1、主库
SET @@GLOBAL.ENFORCE_GTID_CONSISTENCY = WARN; SET @@GLOBAL.ENFORCE_GTID_CONSISTENCY = ON; SET @@GLOBAL.GTID_MODE = OFF_PERMISSIVE; SET @@GLOBAL.GTID_MODE = ON_PERMISSIVE; SHOW STATUS LIKE 'ONGOING_ANONYMOUS_TRANSACTION_COUNT'; SET @@GLOBAL.GTID_MODE = ON;
3.7.2、从库
SET @@GLOBAL.ENFORCE_GTID_CONSISTENCY = WARN; SET @@GLOBAL.ENFORCE_GTID_CONSISTENCY = ON; SET @@GLOBAL.GTID_MODE = OFF_PERMISSIVE; SET @@GLOBAL.GTID_MODE = ON_PERMISSIVE; SHOW STATUS LIKE 'ONGOING_ANONYMOUS_TRANSACTION_COUNT'; SET @@GLOBAL.GTID_MODE = ON; stop replica; CHANGE REPLICATION SOURCE TO SOURCE_AUTO_POSITION = 1 ; START replica;
3.7.3、修改配置文件
gtid_mode = ON enforce_gtid_consistency = ON
3.7.4、主节点模拟增量数据产生及数据同步
CREATE TABLE t_city_list( id bigint auto_increment, country VARCHAR(64), city VARCHAR(64), constraint pk_t_city_list_id primary key(id) ); INSERT INTO t_city_list(country,city) VALUES ('中国','北京'); INSERT INTO t_city_list(country,city) VALUES ('中国','广州'); INSERT INTO t_city_list(country,city) VALUES ('中国','深圳'); INSERT INTO t_city_list(country,city) VALUES ('中国','香港'); INSERT INTO t_city_list(country,city) VALUES ('中国','上海'); INSERT INTO t_city_list(country,city) VALUES ('日本','东京'); INSERT INTO t_city_list(country,city) VALUES ('日本','大阪'); commit; select * from db01.t_city_list
从节点检查
select * from t_city_list;
主节点再次新增数据
INSERT INTO t_city_list(country,city) VALUES ('中国','中山'); INSERT INTO t_city_list(country,city) VALUES ('中国','珠海');
从节点再次检查数据同步情况
select * from t_city_list;
3.8、主备切换
3.8.1、设置主库只读
set global read_only=ON; set global super_read_only=ON;
3.8.2、查看从库进程状态
show replica status \G;
确认参数项
Slave_IO_Running 参数值为YES
Slave_SQL_Running 参数值为YES
Seconds_Behind_Master 参数值为 0
3.8.3、主备节点两边的executed_gtid集合对比
select @@global.gtid_executed; +-------------------------------------------+ | @@global.gtid_executed | +-------------------------------------------+ | e9adc552-10d5-11ef-81ba-000c2940f616:1-10 | +-------------------------------------------+ 1 row in set (0.00 sec) show global variables like 'gtid_%'; +----------------------------------+-------------------------------------------+ | Variable_name | Value | +----------------------------------+-------------------------------------------+ | gtid_executed | e9adc552-10d5-11ef-81ba-000c2940f616:1-10 | | gtid_executed_compression_period | 0 | | gtid_mode | ON | | gtid_owned | | | gtid_purged | | +----------------------------------+-------------------------------------------+ 5 rows in set (0.00 sec)
3.8.4、从库停掉复制进程并清空主从信息(原从库)
stop replica; reset replica all;
3.8.5、从库关闭只读(原从库)
set global read_only=off; set global super_read_only=off;
3.8.6、主库设置执行原主库转为从库
CHANGE MASTER TO MASTER_HOST='192.168.80.51',MASTER_PORT=3306, MASTER_USER='repl', MASTER_PASSWORD='Root@3306',master_auto_position=1; #mysql5.7/mysql8.0 CHANGE REPLICATION SOURCE TO SOURCE_HOST='192.168.80.51',SOURCE_PORT=3306,SOURCE_USER='repl', SOURCE_PASSWORD='Root@3306',SOURCE_auto_position=1; #mysql8.4 start replica;
3.8.7、检查验证
新从节点原主节点80.50
show replica status \G;
新主节点原从节点80.51
INSERT INTO db01.t_city_list(country,city) VALUES ('中国','兰州');
新主从节点检查数据同步情况
select * from db01.t_city_list;
到此这篇关于MySQL8.4一主一从环境搭建实现的文章就介绍到这了,更多相关MySQL8.4一主一从搭建内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!