MySQL主从复制使用方法 (2)

mysql命令

CHANGE MASTER TO MASTER_HOST= 'master_host_ip', MASTER_USER= 'repl', MASTER_PASSWORD = 'password', MASTER_LOG_FILE='mysql_log_file_name', MASTER_LOG_POS=xxxxxx; 5. 启动基于GTID的复制链路

GTID:全局事务ID
GTID可以保证每一个在主上提交的事务,在复制集群中可以生成一个唯一的ID值,要使用基于GTID的复制,我们要在主从复制的配置文件中同时加入以下配置项

mysql配置

gtid_mode=on # 是否启动gtid模式,启动了此模式会在二进制日志中会额外记录每个事务的GTID标识符 enforce-gtid-consistency # 强制gtid一致性,用于保证启动gtid后事务的安全 log-slave-updates = on # mysql5.6一定要启用参数,5.7可以不启用

mysql命令

CHANGE MASTER TO MASTER_HOST= 'master_host_ip', MASTER_USER= 'repl', MASTER_PASSWORD = 'password', MASTER_AUTO_POSITION=1; GTID复制的限制

无法再使用create table ... select 语句建立表,只能先create表,再insert 数据
无法在事务中使用create temporary table 建立临时表
无法使用关联更新同时更新事务表和非事务表

4和5中选一个执行即可 五. mysql主从复制演示 1. 先对主服务器进行配置 [client] port = 3306 # 客户端端口号为3306 socket = /home/mysql/data/mysql.sock [mysqld] # skip # skip_name_resolve = 1 skip-external-locking =1 # GENERAL # user = mysql # MySQL启动用户 default_storage_engine = InnoDB # 新数据表的默认数据表类型 character-set-server = utf8 # #服务端默认编码(数据库级别) socket = /home/mysql/data/mysql.sock pid_file = /home/mysql/data/mysqld.pid basedir = /home/mysql #使用该目录作为根目录(Mysql安装目录); port = 3306 bind-address = 0.0.0.0 log_error_verbosity = 3 explicit_defaults_for_timestamp = off #sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES #sql_mode = NO_ENGINE_SUBSTITUTION # undo log # innodb_undo_directory = /home.mysql/undo # innodb_undo_tablespaces = 32 # MyISAM # key_buffer_size =32M # SAFETY # max_allowed_packet = 100M max_connect_errors = 1000000 sysdate_is_now =1 #innodb = FORCE #innodb_strict_mode = 1 # Replice # server-id = 100 relay_log = /home/mysql/sql_log/mysqld-relay-bin #plugin-load = semisync_master.so log_slave_updates = on master_info_repository = TABLE relay_log_info_repository =TABLE # gtid_mode = on # enforce_gtid_consistency =on # skip-slave-start =1 #rpl_semi_sync_master_enabled = 1 #rpl_semi_sync_master_timeout=200 # 0.2 second master_info_respository = TABLE # gtid_mode= on # enforce_gtid_consistency = on # skip-slave-start = 1 # DATA STORAGE # datadir = /home/mysql/data #mysql 数据文件存放的目录 tmpdir = /tmp # MySQL存放临时文件的目录 # BINARY LOGGING # log_bin = /home/mysql/sql_log/mysql-bin max_binlog_size = 1000M binlog_format = row expire_log_days = 7 sync_binlog = 1 # CACHES AND LIMITS # tmp_table_size = 32M max_heap_table_size = 32M query_cache_type = 0

由于主服务器一直在运行着,在生产环境中主服务器是很少会重启的,如果主服务器重启,会造成正常的业务访问的中断,所以在服务器启动之前就启动了二进制日志
这里不需要重启主服务器了,由于主服务器的默认server_id=1,我们虽然在配置文件中更改了它的值 ,但实际运行环境中并没有改变

我们可以查看一下当前server_id

mysql> show variables like '%server_id%';

可以通过以下命令动态的进行修改

mysql> set global server_id = 100; 2. 再对从服务器进行配置 [client] port = 3306 # 客户端端口号为3306 socket = /home/mysql/data/mysql.sock [mysqld] # skip # skip_name_resolve = 1 skip-external-locking =1 # GENERAL # user = mysql # MySQL启动用户 default_storage_engine = InnoDB # 新数据表的默认数据表类型 character-set-server = utf8 # #服务端默认编码(数据库级别) socket = /home/mysql/data/mysql.sock pid_file = /home/mysql/data/mysqld.pid basedir = /home/mysql #使用该目录作为根目录(Mysql安装目录); port = 3306 bind-address = 0.0.0.0 log_error_verbosity = 3 explicit_defaults_for_timestamp = off #sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES #sql_mode = NO_ENGINE_SUBSTITUTION read_only = on # undo log # innodb_undo_directory = /home.mysql/undo # innodb_undo_tablespaces = 32 # MyISAM # key_buffer_size =32M # SAFETY # max_allowed_packet = 100M max_connect_errors = 1000000 sysdate_is_now =1 #innodb = FORCE #innodb_strict_mode = 1 # Replice # server-id = 101 relay_log = /home/mysql/sql_log/mysqld-relay-bin #plugin-load = semisync_master.so log_slave_updates = on master_info_repository = TABLE relay_log_info_repository =TABLE # gtid_mode = on # enforce_gtid_consistency =on # skip-slave-start =1 #rpl_semi_sync_master_enabled = 1 #rpl_semi_sync_master_timeout=200 # 0.2 second master_info_respository = TABLE # gtid_mode= on # enforce_gtid_consistency = on # skip-slave-start = 1 # DATA STORAGE # datadir = /home/mysql/data #mysql 数据文件存放的目录 tmpdir = /tmp # MySQL存放临时文件的目录 # BINARY LOGGING # log_bin = /home/mysql/sql_log/mysql-bin max_binlog_size = 1000M binlog_format = row expire_log_days = 7 sync_binlog = 1 # CACHES AND LIMITS # tmp_table_size = 32M max_heap_table_size = 32M query_cache_type = 0

修改完从服务器配置后,重启mysql服务器

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpxxzp.html