1. 官方下载yum源
https://dev.mysql.com/downloads/repo/yum/
2. 安装yum源
2.1. 安装前先卸载mysql
# 查看是否存在历史安装
yum list installed mysql*
# 删除安装包
yum remove mysql-community-client mysql-community-common mysql-community-libs mysql-community-libs-compat mysql-community-server mysql57-community-release
# 删除配置文件
rm -rf var/lib/mysqlrm /etc/my.cnf
2.1. 安装mysql
rpm -ivhmysql80-community-release-el7-7.noarch.rpm
3 安装mysql8.0.31
yum -y install mysql-community-server
# 启动命令
systemctl start mysqld
4 查看mysql默认初始密码
grep 'temporary password' var/log/mysqld.log./mysql -uroot -p'<6nC&pSfg9Du'mysql> alter user 'root'@'localhost' identified by "Shark@666";mysql> use mysqlmysql> update user set host='%' where host='localhost';mysql> flush privileges;
5 在主库端创建复制用户
mysql>create user 'Repl'@'%' WITH sha256_password BY 'Shark@888';mysql>GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'Repl'@'%';
6 安装半同步插件
mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';mysql> show variables like '%semi%';+-------------------------------------------+------------+--------------------------+| Variable_name | Value | 中文解释 |+-------------------------------------------+------------+--------------------------+| rpl_semi_sync_master_enabled | OFF | 开启半同步 || rpl_semi_sync_master_timeout | 10000 | 毫秒 超时 || rpl_semi_sync_master_trace_level | 32 | 跟踪层次 || rpl_semi_sync_master_wait_for_slave_count | 1 | 等待从库数量 || rpl_semi_sync_master_wait_no_slave | ON |要等待丛库确认收到 || rpl_semi_sync_master_wait_point | AFTER_SYNC |到从库前不被其他会话可见 |+-------------------------------------------+------------+--------------------------+6 rows in set (0.00 sec)mysql> exit
7 设置开机自启
systemctl enable mysqld
systemctl status mysqld
systemctl restart mysqld
systemctl daemon-reload # 刷新systemctl配置
8 关闭系统安全
vim /etc/selinux/configSELINUX=disabledchown -R mysql:mysql var/lib/mysqlsystemctl disable firewalldsystemctl stop firewalld
9 主库端编辑参数文件
vim etc/my.cnf[mysqld]datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.socklog-error=/var/log/mysqld.logpid-file=/var/run/mysqld/mysqld.pidport = 3306character_set_server=utf8mb4init_connect='SET NAMES utf8mb4'log_timestamps = SYSTEMdefault-time-zone = '+8:00'skip-name-resolveinnodb_flush_log_at_trx_commit=1sync_binlog=1#Limitmax_connections =100# PerThreadBySessionMemOptionjoin_buffer_size = 8Msort_buffer_size = 8Mread_rnd_buffer_size = 8Minnodb_buffer_pool_size = 128Mbinlog_cache_size= 8M#Masterserver_id=135 #服务器idgtid_mode=on #开启gtid模式enforce_gtid_consistency=on #强制gtid一致性,开启后对于特定create table不被支持log_bin=Master_binbinlog_format=row #强烈建议,其他格式可能造成数据不一致max_binlog_size = 1024Mbinlog_expire_logs_seconds= 172800 #tow daybinlog-ignore-db=information_schemabinlog-ignore-db=clusterbinlog-ignore-db=mysqlbinlog-do-db=sharkdb#SemiSyncOptionrpl_semi_sync_master_enabled = ONrpl_semi_sync_master_timeout=10000 ##毫秒 Master Waite Salve tiemoutrpl_semi_sync_master_wait_no_slave=ON ##表示MASTER事务都要等待丛库确认收到
# 从库参数文件
vim etc/my.cnf[mysqld]datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.socklog-error=/var/log/mysqld.logpid-file=/var/run/mysqld/mysqld.pidport = 3306character_set_server=utf8mb4init_connect='SET NAMES utf8mb4'log_timestamps = SYSTEMdefault-time-zone = '+8:00'skip-name-resolveinnodb_flush_log_at_trx_commit=1sync_binlog=1#Limitmax_connections =100# PerThreadBySessionMemOptionjoin_buffer_size = 8Msort_buffer_size = 8Mread_rnd_buffer_size = 8Minnodb_buffer_pool_size = 128Mbinlog_cache_size= 8Mserver_id=136 #服务器idgtid_mode=on #开启gtid模式enforce_gtid_consistency=on #强制gtid一致性,开启后对于特定create table不被支持log_bin=SALVE01_binbinlog_format=row #强烈建议,其他格式可能造成数据不一致max_binlog_size = 1024Mbinlog_expire_logs_seconds= 172800 #tow day#Salverelay_log=Relay01Logskip_slave_start=1 # skip_replica_startlog_slave_updates=1 #log_replica_updatesslave-parallel-type=LOGICAL_CLOCK #replica_parallel_typeslave_parallel_workers=16 #开启多线程复制master_info_repository=TABLErelay_log_info_repository=TABLErelay_log_recovery=ONread_only=ONsuper_read_only = OFFslave_net_timeout=60sync_master_info=1000 #MasterInfo Per 1000 Event SyncTo Tablesync_relay_log=1000 #RelayLog Per 1000 Event Sync LogFilesync_relay_log_info=100 #RelayLogInfo Per 1000 Event Syncto Tableskip_slave_start = OFF # on:Skip 'start savle' by mysqld start ; Off BY STARTreplicate_do_db=sharkdb
#--主从都重启
[root@centos7-mysql8-master ~]# service mysqld start
10 从库配置复制参数
mysql> change master to master_host = '192.168.2.31', master_port = 3306, master_user = 'Repl', master_password = 'Shark@888', master_auto_position = 1;mysql> start slave;Query OK, 0 rows affected, 1 warning (3.59 sec)mysql> show slave status\G;*************************** 1. row ***************************Slave_IO_State: Waiting for source to send eventMaster_Host: 192.168.2.31Master_User: ReplMaster_Port: 3306Connect_Retry: 60Master_Log_File: Master_bin.000010Read_Master_Log_Pos: 460Relay_Log_File: RelayLog.000002Relay_Log_Pos: 678Relay_Master_Log_File: Master_bin.000010Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: sharkdbReplicate_Ignore_DB:Replicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno: 0Last_Error:Skip_Counter: 0Exec_Master_Log_Pos: 460Relay_Log_Space: 881Until_Condition: NoneUntil_Log_File:Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File:Master_SSL_CA_Path:Master_SSL_Cert:Master_SSL_Cipher:Master_SSL_Key:Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error:Last_SQL_Errno: 0Last_SQL_Error:Replicate_Ignore_Server_Ids:Master_Server_Id: 135Master_UUID: 61c4b1bd-7331-11ed-8ac9-0800279840ebMaster_Info_File: mysql.slave_master_infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Replica has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind:Last_IO_Error_Timestamp:Last_SQL_Error_Timestamp:Master_SSL_Crl:Master_SSL_Crlpath:Retrieved_Gtid_Set: 61c4b1bd-7331-11ed-8ac9-0800279840eb:1Executed_Gtid_Set: 61c4b1bd-7331-11ed-8ac9-0800279840eb:1Auto_Position: 1Replicate_Rewrite_DB:Channel_Name:Master_TLS_Version:Master_public_key_path:Get_master_public_key: 0Network_Namespace:1 row in set, 1 warning (0.00 sec)ERROR:No query specifiedmysql> show master status;+---------------------+----------+--------------+------------------+------------------------------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+---------------------+----------+--------------+------------------+------------------------------------------+| Salve_01_bin.000001 | 585 | | | 61c4b1bd-7331-11ed-8ac9-0800279840eb:1-2 |+---------------------+----------+--------------+------------------+------------------------------------------+1 row in set (0.00 sec)show global status like "%sync%";+--------------------------------------------+-------+| Variable_name | Value |+--------------------------------------------+-------+| Innodb_data_fsyncs | 259 || Innodb_data_pending_fsyncs | 0 || Innodb_os_log_fsyncs | 91 || Innodb_os_log_pending_fsyncs | 0 || Rpl_semi_sync_master_clients | 0 || Rpl_semi_sync_master_net_avg_wait_time | 0 || Rpl_semi_sync_master_net_wait_time | 0 || Rpl_semi_sync_master_net_waits | 0 || Rpl_semi_sync_master_no_times | 1 || Rpl_semi_sync_master_no_tx | 1 || Rpl_semi_sync_master_status | OFF || Rpl_semi_sync_master_timefunc_failures | 0 || Rpl_semi_sync_master_tx_avg_wait_time | 0 || Rpl_semi_sync_master_tx_wait_time | 0 || Rpl_semi_sync_master_tx_waits | 0 || Rpl_semi_sync_master_wait_pos_backtraverse | 0 || Rpl_semi_sync_master_wait_sessions | 0 || Rpl_semi_sync_master_yes_tx | 0 |+--------------------------------------------+-------+18 rows in set (0.01 sec)
11 同步测试
##主库操作
mysql> show master status;+-------------------+----------+--------------+----------------------------------+----------------------------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+-------------------+----------+--------------+----------------------------------+----------------------------------------+| Master_bin.000010 | 460 | sharkdb | information_schema,cluster,mysql | 61c4b1bd-7331-11ed-8ac9-0800279840eb:1 |+-------------------+----------+--------------+----------------------------------+----------------------------------------+1 row in set (0.00 sec)mysql> create database sharkdb;Query OK, 1 row affected (0.55 sec)
##从库查看
mysql> show slave status\G;*************************** 1. row ***************************Slave_IO_State: Waiting for source to send eventMaster_Host: 192.168.2.31Master_User: ReplMaster_Port: 3306Connect_Retry: 60Master_Log_File: Master_bin.000010Read_Master_Log_Pos: 654Relay_Log_File: RelayLog.000002Relay_Log_Pos: 872Relay_Master_Log_File: Master_bin.000010Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: sharkdbLast_Errno: 0Skip_Counter: 0Exec_Master_Log_Pos: 654Relay_Log_Space: 1075Until_Condition: NoneUntil_Log_Pos: 0Master_SSL_Allowed: NoSeconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_SQL_Errno: 0Replicate_Ignore_Server_Ids:Master_Server_Id: 135Master_UUID: 61c4b1bd-7331-11ed-8ac9-0800279840ebMaster_Info_File: mysql.slave_master_infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Replica has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Retrieved_Gtid_Set: 61c4b1bd-7331-11ed-8ac9-0800279840eb:1-2Executed_Gtid_Set: 61c4b1bd-7331-11ed-8ac9-0800279840eb:1-2Auto_Position: 1Get_master_public_key: 01 row in set, 1 warning (0.01 sec)ERROR:No query specifiedmysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || sharkdb || sys |+--------------------+5 rows in set (0.02 sec)
##主库创建表测试
mysql> use sharkdbDatabase changedmysql> CREATE TABLE `big_table` (`uid` int(11) NOT NULL AUTO_INCREMENT,`mobile` char(11) DEFAULT NULL,`passwd` varchar(50) DEFAULT NULL,`name` varchar(50) DEFAULT NULL,`sex` tinyint DEFAULT NULL,`birthday` datetime DEFAULT NULL,`updated_time` datetime DEFAULT NULL,PRIMARY KEY (`uid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
#从库查看
mysql> show tables;+-------------------+| Tables_in_sharkdb |+-------------------+| big_table |+-------------------+1 row in set (0.01 sec)mysql> desc big_table;+--------------+-------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+--------------+-------------+------+-----+---------+----------------+| uid | int | NO | PRI | NULL | auto_increment || mobile | char(11) | YES | | NULL | || passwd | varchar(50) | YES | | NULL | || name | varchar(50) | YES | | NULL | || sex | tinyint | YES | | NULL | || birthday | datetime | YES | | NULL | || updated_time | datetime | YES | | NULL | |+--------------+-------------+------+-----+---------+----------------+7 rows in set (0.01 sec)
文章转载自海鲨数据库架构师,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




