当前位置:   article > 正文

Linux mysql5.7自带的主从复制功能配置使用

Linux mysql5.7自带的主从复制功能配置使用

主库IP:192.168.164.21

从库IP:192.168.164.22

事先在两台服务器上安装好mysql5.7

Master库配置

1、修改/etc/my.cnf文件

将一下内容覆盖到该文件中

 1)首先需要修改文件中的server-id,保证唯一性,并且master库的该值一定要小于slave库

      它是mysql服务的唯一标识,如果时单机模式的话不需要修改,可以时任何值,如果时集群模式,需要修改,要保证它的唯一性

 2)修改log_bin 值:为master_log 

      日志文件命名, 开启日志功能。此日志是命令日志。就是记录主库中执行的所有的 SQL 命令的

      MySQL的log_bin不是执行日志,状态日志. 是操作日志.就是在DBMS中所有的SQL命令 log_bin 日志不是必要的.只有配置主从备份时才必要

变量的值就是日志文件名称.是日志文件名称的主体. MySQL 数据库自动增加文件名后缀和文件类型.
3)保存重启mysql

  1. [client]
  2. port = 3306
  3. default-character-set = utf8mb4
  4. [mysqld]
  5. port = 3306
  6. user = mysql
  7. bind-address = 0.0.0.0
  8. server-id = 1
  9. init-connect = 'SET NAMES utf8mb4'
  10. character-set-server = utf8mb4
  11. skip-name-resolve
  12. skip-external-locking
  13. #skip-networking
  14. back_log = 300
  15. max_connections = 1000
  16. max_connect_errors = 6000
  17. open_files_limit = 65535
  18. table_open_cache = 128
  19. max_allowed_packet = 4M
  20. binlog_cache_size = 1M
  21. max_heap_table_size = 8M
  22. tmp_table_size = 16M
  23. read_buffer_size = 2M
  24. read_rnd_buffer_size = 8M
  25. sort_buffer_size = 8M
  26. join_buffer_size = 8M
  27. key_buffer_size = 4M
  28. thread_cache_size = 8
  29. query_cache_type = 1
  30. query_cache_size = 8M
  31. query_cache_limit = 2M
  32. ft_min_word_len = 4
  33. log_bin = master_log
  34. binlog_format = mixed
  35. expire_logs_days = 10
  36. slow_query_log = 1
  37. long_query_time = 1
  38. performance_schema = 0
  39. explicit_defaults_for_timestamp
  40. lower_case_table_names = 1
  41. default_storage_engine = InnoDB
  42. #default-storage-engine = MyISAM
  43. innodb_file_per_table = 1
  44. innodb_open_files = 500
  45. innodb_buffer_pool_size = 64M
  46. innodb_write_io_threads = 4
  47. innodb_read_io_threads = 4
  48. innodb_thread_concurrency = 0
  49. innodb_purge_threads = 1
  50. innodb_flush_log_at_trx_commit = 2
  51. innodb_log_buffer_size = 2M
  52. innodb_log_file_size = 32M
  53. innodb_log_files_in_group = 3
  54. innodb_max_dirty_pages_pct = 90
  55. innodb_lock_wait_timeout = 120
  56. bulk_insert_buffer_size = 8M
  57. myisam_sort_buffer_size = 8M
  58. myisam_max_sort_file_size = 10G
  59. myisam_repair_threads = 1
  60. interactive_timeout = 28800
  61. wait_timeout = 28800
  62. [mysqldump]
  63. quick
  64. max_allowed_packet = 16M
  65. [myisamchk]
  66. key_buffer_size = 8M
  67. sort_buffer_size = 8M
  68. read_buffer = 4M
  69. write_buffer = 4M

文件内容属性说明如下

  1. [client] mysql -uroot -proot
  2. port = 3306 客户端端口
  3. default-character-set = utf8mb4 客户端默认字符集
  4. [mysqld] 服务器
  5. port = 3306 端口
  6. user = mysql 提供简单用户数据
  7. bind-address = 0.0.0.0 当前用户可访问的主机IP, localhost. mysql用户只能在本地访问.
  8. server-id = 1 服务器的唯一标识
  9. init-connect = 'SET NAMES utf8mb4' 初始化数据库链接时提供的配置信息
  10. character-set-server = utf8mb4 服务器基本字符集
  11. skip-name-resolve
  12. skip-external-locking
  13. #skip-networking
  14. back_log = 300
  15. max_connections = 1000 最大客户端连接数
  16. max_connect_errors = 6000 单次连接最大可能出现的错误数.
  17. open_files_limit = 65535
  18. table_open_cache = 128 表的开启基础缓存. 单位是个数
  19. max_allowed_packet = 4M
  20. binlog_cache_size = 1M
  21. max_heap_table_size = 8M
  22. tmp_table_size = 16M
  23. read_buffer_size = 2M 读缓存大小
  24. read_rnd_buffer_size = 8M
  25. sort_buffer_size = 8M
  26. join_buffer_size = 8M
  27. key_buffer_size = 4M
  28. thread_cache_size = 8
  29. query_cache_type = 1
  30. query_cache_size = 8M
  31. query_cache_limit = 2M
  32. ft_min_word_len = 4
  33. log_bin = mysql-bin 日志文件名称.文件名的主体内容.mysql会自动提供后缀和文件类型.
  34. binlog_format = mixed
  35. expire_logs_days = 10
  36. slow_query_log = 1
  37. long_query_time = 1
  38. performance_schema = 0
  39. explicit_defaults_for_timestamp
  40. lower_case_table_names = 1
  41. default_storage_engine = InnoDB 默认数据库引擎
  42. #default-storage-engine = MyISAM
  43. innodb_file_per_table = 1
  44. innodb_open_files = 500
  45. innodb_buffer_pool_size = 64M
  46. innodb_write_io_threads = 4
  47. innodb_read_io_threads = 4
  48. innodb_thread_concurrency = 0
  49. innodb_purge_threads = 1
  50. innodb_flush_log_at_trx_commit = 2
  51. innodb_log_buffer_size = 2M
  52. innodb_log_file_size = 32M
  53. innodb_log_files_in_group = 3
  54. innodb_max_dirty_pages_pct = 90
  55. innodb_lock_wait_timeout = 120
  56. bulk_insert_buffer_size = 8M
  57. myisam_sort_buffer_size = 8M
  58. myisam_max_sort_file_size = 10G
  59. myisam_repair_threads = 1
  60. interactive_timeout = 28800
  61. wait_timeout = 28800
  62. [mysqldump] 安全提供.
  63. quick
  64. max_allowed_packet = 16M
  65. [myisamchk] 数据库额外辅助功能配置, 如缓存,索引缓存等.
  66. key_buffer_size = 8M
  67. sort_buffer_size = 8M
  68. read_buffer = 4M
  69. write_buffer = 4M

2、创建用户(用于slave访问master的用户)

在 MySQL 数据库中,为不存在的用户授权,就是同步创建用户并授权. 此用户是从库访问主库使用的用户 ip 地址不能写为%. 因为主从备份中,当前创建的用户,是给从库 Slave 访问主库 Master 使用的.用户必须有指定的访问地址.不能是通用地址. 

命令:grant all privileges on *.* to 'slave'@'192.168.164.22' identified by 'slave' with grant option; 

           flush  privileges;

Slave库配置

1、配置/etc/my.cnf中的server-id为2

2、log_bin

可以使用默认配置, 也可以注释.

3、修改 uuid

主从模式要求多个 MySQL 物理名称不能相同. 即按装 MySQL 过程中 Linux 自动生成的 物理标志. 唯一物理标志命名为 uuid. 保存位置是 MySQL 数据库的数据存放位置. 默认为 /var/lib/mysql 目录中. 文件名是 auto.cnf. 修改 auto.cnf 文件中的 uuid 数据. 随意修改,不建议改变数据长度.建议改变数据内容. /var/lib/mysql/auto.cnf

4、重启mysql

5、登入mysql:mysql -uroot -p123456

6、停掉slave功能:stop slave;

7、配置主库信息

需要修改的数据是依据Master信息修改的.ip是Master所在物理机IP. 用户名和密码是 Master 提供的 Slave 访问用户名和密码. 日志文件是在 Master 中查看的主库信息提供的.在 Master 中使用命令 showmasterstatus 查看日志文件名称.

命令:change master to master_host='192.168.164.21', master_user='slave', master_password='slave',master_log_file='master_log.000001';

8、启动slave功能:start slave;

9、查看slave状态:show slave status \G;

  1. mysql> show slave status \G;
  2. *************************** 1. row ***************************
  3. Slave_IO_State: Waiting for master to send event
  4. Master_Host: 192.168.164.21
  5. Master_User: slave
  6. Master_Port: 3306
  7. Connect_Retry: 60
  8. Master_Log_File: master_log.000001
  9. Read_Master_Log_Pos: 154
  10. Relay_Log_File: job-dangdai-node-2-relay-bin.000002
  11. Relay_Log_Pos: 369
  12. Relay_Master_Log_File: master_log.000001
  13. Slave_IO_Running: Yes
  14. Slave_SQL_Running: Yes
  15. Replicate_Do_DB:
  16. Replicate_Ignore_DB:
  17. Replicate_Do_Table:
  18. Replicate_Ignore_Table:
  19. Replicate_Wild_Do_Table:
  20. Replicate_Wild_Ignore_Table:
  21. Last_Errno: 0
  22. Last_Error:
  23. Skip_Counter: 0
  24. Exec_Master_Log_Pos: 154
  25. Relay_Log_Space: 589
  26. Until_Condition: None
  27. Until_Log_File:
  28. Until_Log_Pos: 0
  29. Master_SSL_Allowed: No
  30. Master_SSL_CA_File:
  31. Master_SSL_CA_Path:
  32. Master_SSL_Cert:
  33. Master_SSL_Cipher:
  34. Master_SSL_Key:
  35. Seconds_Behind_Master: 0
  36. Master_SSL_Verify_Server_Cert: No
  37. Last_IO_Errno: 0 最后一次错误的 IO 请求编号
  38. Last_IO_Error:
  39. Last_SQL_Errno: 0 最后一次错误的执行 SQL 命令编号
  40. Last_SQL_Error:
  41. Replicate_Ignore_Server_Ids:
  42. Master_Server_Id: 1
  43. Master_UUID: 53062ab6-b535-11e9-bd3c-000c294f0d13
  44. Master_Info_File: /var/lib/mysql/master.info
  45. SQL_Delay: 0
  46. SQL_Remaining_Delay: NULL
  47. Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
  48. Master_Retry_Count: 86400
  49. Master_Bind:
  50. Last_IO_Error_Timestamp:
  51. Last_SQL_Error_Timestamp:
  52. Master_SSL_Crl:
  53. Master_SSL_Crlpath:
  54. Retrieved_Gtid_Set:
  55. Executed_Gtid_Set:
  56. Auto_Position: 0
  57. Replicate_Rewrite_DB:
  58. Channel_Name:
  59. Master_TLS_Version:
  60. 1 row in set (0.01 sec)
  61. ERROR:
  62. No query specified

测试:

 

主库

创建表

新增一条数据

 

从库

查看test_table

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/964831
推荐阅读
相关标签
  

闽ICP备14008679号