挂载目录跟配置文件- 8.0.22

https://blog.csdn.net/u013068184/article/details/107691389?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-1.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-1.pc_relevant_default&utm_relevant_index=2

启动命令

  1. # master shell
  2. docker run -p 7001:3306 --name mysql_master --privileged=true \
  3. -v /tn/docker-conf/mysql/mysql-master/log:/var/log/mysql \
  4. -v /tn/docker-conf/mysql/mysql-master/data:/var/lib/mysql \
  5. -v /tn/docker-conf/mysql/mysql-master/conf:/etc/mysql \
  6. -v /tn/docker-conf/mysql/mysql-master/mysql-files:/var/lib/mysql-files \
  7. -e lower_case_table_names=1 \
  8. -e MYSQL_ROOT_PASSWORD=root \
  9. -d mysql:8.0.26
  10. # master cmd
  11. docker run -p 7001:3306 --name mysql_master --privileged=true -v /D/docker/docker-conf/mysql/mysql-master/log:/var/log/mysql -v /D/docker/docker-conf/mysql/mysql-master/data:/var/lib/mysql -v /C/Users/docker-conf/mysql/mysql-master:/etc/mysql -v /D/docker/docker-conf/mysql/mysql-master/mysql-files:/var/lib/mysql-files -e lower_case_table_names=1 -e MYSQL_ROOT_PASSWORD=root -d mysql:8.0.26
  12. #slave shell
  13. docker run -p 7002:3306 --name mysql_slave --privileged=true \
  14. -v /tn/docker-conf/mysql/mysql-slave/log:/var/log/mysql \
  15. -v /tn/docker-conf/mysql/mysql-slave/data:/var/lib/mysql \
  16. -v /tn/docker-conf/mysql/mysql-slave/conf:/etc/mysql \
  17. -v /tn/docker-conf/mysql/mysql-slave/mysql-files:/var/lib/mysql-files \
  18. -e lower_case_table_names=1 \
  19. -e MYSQL_ROOT_PASSWORD=root \
  20. -d mysql:8.0.26
  21. #slave cmd
  22. docker run -p 7002:3306 --name mysql_slave --privileged=true -v /D/docker/docker-conf/mysql/mysql-slave/log:/var/log/mysql -v /D/docker/docker-conf/mysql/mysql-slave/data:/var/lib/mysql -v /C/Users/docker-conf/mysql/mysql-slave:/etc/mysql -v /D/docker/docker-conf/mysql/mysql-slave/mysql-files:/var/lib/mysql-files -e lower_case_table_names=1 -e MYSQL_ROOT_PASSWORD=root -d mysql:8.0.26

mysql_master/conf/my.cnf

  1. [mysqld]
  2. pid-file = /var/run/mysqld/mysqld.pid
  3. socket = /var/run/mysqld/mysqld.sock
  4. datadir = /var/lib/mysql
  5. secure-file-priv= NULL
  6. default_authentication_plugin=mysql_native_password
  7. default-storage-engine=INNODB
  8. character-set-server=utf8mb4
  9. collation-server=utf8mb4_unicode_ci
  10. init_connect='SET NAMES utf8mb4'
  11. # 主从复制链路(局域网)中服务id不能相同
  12. server-id=101
  13. # 启用二进制日志
  14. log-bin=mysql-master-bin
  15. # 不开启binlog的数据库
  16. binlog-ignore-db=information_schema
  17. binlog-ignore-db=sys
  18. binlog-ignore-db=performance_schema
  19. binlog-ignore-db=mysql
  20. # 开启binlog的数据库,不能用逗号隔开
  21. # binlog-do-db=demo
  22. # binlog-do-db=db1
  23. # binlog-do-db=db2
  24. # binlog-do-db=db3
  25. # binlog格式 mixed statement row
  26. binlog_format=mixed
  27. # binlog最大大小,超过这个值会创建新的binlog
  28. # max_binlog_size=32m
  29. # binlog缓存大小,执行sql的时候会创建缓存,所有session的内存超过最大值,会报错
  30. binlog_cache_size=1m
  31. # binlog最大缓存大小
  32. max_binlog_cache_size=128m
  33. # 过期清理 默认0,表示不清理
  34. expire_logs_days=7
  35. # 跳过错误,避免slave端复制中断
  36. slave_skip_errors=1062
  37. [client]
  38. default-character-set=utf8mb4
  39. [mysql]
  40. default-character-set=utf8mb4
  41. # Custom config should go here
  42. !includedir /etc/mysql/conf.d/

mysql_slave/conf/my.cnf

  1. [mysqld]
  2. pid-file = /var/run/mysqld/mysqld.pid
  3. socket = /var/run/mysqld/mysqld.sock
  4. datadir = /var/lib/mysql
  5. secure-file-priv= NULL
  6. default_authentication_plugin=mysql_native_password
  7. default-storage-engine=INNODB
  8. character-set-server=utf8mb4
  9. collation-server=utf8mb4_unicode_ci
  10. init_connect='SET NAMES utf8mb4'
  11. # 服务id,不能与其他主、从服务的id相同
  12. server-id=102
  13. # 启用二进制日志
  14. log-bin=mysql-slave1-bin
  15. # 主从服复制忽略的数据库
  16. binlog_ignore_db=information_schema
  17. binlog_ignore_db=sys
  18. binlog_ignore_db=performance_schema
  19. binlog_ignore_db=mysql
  20. # binlog格式 mixed statement row
  21. binlog_format=mixed
  22. # binlog最大大小,超过这个值会创建新的binlog
  23. # max_binlog_size=32m
  24. # binlog缓存大小,执行sql的时候会创建缓存,所有session的内存超过最大值,会报错
  25. binlog_cache_size=1m
  26. # binlog最大缓存大小
  27. max_binlog_cache_size=128m
  28. # 过期清理 默认0,表示不清理
  29. expire_logs_days=7
  30. # 跳过错误,避免slave端复制中断
  31. slave_skip_errors=1062
  32. # slave 将复制事件写进自己的二进制日志
  33. log_slave_updates=1
  34. # slave 设置为只读 (super权限除外)
  35. read_only=1
  36. [client]
  37. default-character-set=utf8mb4
  38. [mysql]
  39. default-character-set=utf8mb4
  40. # Custom config should go here
  41. !includedir /etc/mysql/conf.d/

其余步骤

  1. # master (主从最好在创一个用户)
  2. CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
  3. GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';
  4. FLUSH PRIVILEGES;
  5. show master status \G;
  6. # slave
  7. ## 查看主节点容器的内部ip地址, `docker inspect imageID`
  8. ### 我这里用 `docker inspect imageID` 查询的ip用不了,所以使用的时宿主机的IP
  9. -- 1
  10. stop slave; (8.0.22+ stop REPLICA)
  11. -- 2
  12. ## 8.0.23+ masster_ change source_
  13. change master to
  14. master_host='127.0.0.1',
  15. master_user='root',
  16. master_password='root',
  17. master_port=7001,
  18. master_log_file='binlog.000005',
  19. master_log_pos=156,
  20. master_connect_retry=30;
  21. change master to
  22. source_host='127.0.0.1',
  23. source_user='root',
  24. source_password='root',
  25. source_port=7001,
  26. source_log_file='binlog.000005',
  27. source_log_pos=156,
  28. source_connect_retry=30;
  29. -- 3
  30. start slave; (8.0.22+ start REPLICA)
  31. -- 4
  32. ## 况下的查看结果SlaveIORunning 和 SlaveSQLRunning 都是Yes
  33. show slave status \G; (8.0.22+ SHOW REPLICA STATUS;)

手动设置 server_id

server_id 出现冲突的临时处理方法

  1. show variables like '%server_id%';
  2. SET GLOBAL server_id=2;

我出现过一次配置文件中设置server-id不生效的问题

查看配置文件使用信息

mysql —help|grep my.cnf

注意

https://blog.csdn.net/kinginblue/article/details/76863962

windows

mysql: [Warning] World-writable config file ‘/etc/mysql/my.cnf’ is ignored

  1. 配置文件跟data log等目录分开存放,要不然会出权限问题
    1. 配置文件存放到 C:\Users (Windows) or /Users (Mac)
    2. data等其他的文件 !c 盘就行了