谷粒商城创建mysql主从(十三)

Posted 天宇轩-王

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了谷粒商城创建mysql主从(十三)相关的知识,希望对你有一定的参考价值。

361-363  创建mysql主从

STEP 1 启动主从

[root@database ~]#  docker run -p 3307:3306 --name mysql-master -v /mydata/mysql/master/conf:/etc/mysql  -v /mydata/mysql/master/logs:/var/log/mysql  -v /mydata/mysql/master/data:/var/lib/mysql  -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
6a7d36b510b618d8d7c4d408051004153f95644dc4ab09fdcc9d1e1b19d97c5e
[root@database ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
6a7d36b510b6        mysql:5.7           "docker-entrypoint.s…"   4 seconds ago       Up 3 seconds        33060/tcp, 0.0.0.0:3307->3306/tcp   mysql-master
961ab799c1fe        redis               "docker-entrypoint.s…"   12 days ago         Up 11 days          0.0.0.0:6379->6379/tcp              focused_kirch
4dd3eda0035b        a4fdfd462add        "docker-entrypoint.s…"   12 days ago         Up 11 days          0.0.0.0:3306->3306/tcp, 33060/tcp   mysql
[root@database ~]#  docker run -p 3317:3306 --name mysql-slave01 -v /mydata/mysql/slave/conf:/etc/mysql  -v /mydata/mysql/slave/logs:/var/log/mysql  -v /mydata/mysql/slave/data:/var/lib/mysql  -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
991c33239f50e49a0a99ecee5cf9d58b561635dbf688cf6d4359fbdfb4ac9456
[root@database ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                               NAMES
991c33239f50        mysql:5.7           "docker-entrypoint.s…"   14 seconds ago       Up 13 seconds       33060/tcp, 0.0.0.0:3317->3306/tcp   mysql-slave01
6a7d36b510b6        mysql:5.7           "docker-entrypoint.s…"   About a minute ago   Up About a minute   33060/tcp, 0.0.0.0:3307->3306/tcp   mysql-master
961ab799c1fe        redis               "docker-entrypoint.s…"   12 days ago          Up 11 days          0.0.0.0:6379->6379/tcp              focused_kirch
4dd3eda0035b        a4fdfd462add        "docker-entrypoint.s…"   12 days ago          Up 11 days          0.0.0.0:3306->3306/tcp, 33060/tcp   mysql
[

STEP 2 修改配置文件

[root@database ~]# cat /mydata/mysql/master/conf/my.cnf
[client]
default-character-set=utf8

[mysql]
default-character-set=utf8


[mysqld]
init_connect=\'set collation_connection=utf8_unicode_ci\'
init_connect=\'SET NAMES utf8\'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve

server_id=1
log-bin=mysql-bin
read-only=0
binlog-do-db=gulimall_ums
binlog-do-db=gulimall_pms
binlog-do-db=gulimall_oms
binlog-do-db=gulimall_sms
binlog-do-db=gulimall_wms
binlog-do-db=gulimall-admin

replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
[root@database ~]# cat /mydata/mysql/slave/conf/my.cnf
[client]
default-character-set=utf8

[mysql]
default-character-set=utf8


[mysqld]
init_connect=\'set collation_connection=utf8_unicode_ci\'
init_connect=\'SET NAMES utf8\'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve


server_id=2
log-bin=mysql-bin
read-only=1
binlog-do-db=gulimall_ums
binlog-do-db=gulimall_pms
binlog-do-db=gulimall_oms
binlog-do-db=gulimall_sms
binlog-do-db=gulimall_wms
binlog-do-db=gulimall-admin

replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
[root@database ~]#

STEP3 主服务器授权

mysql> grant all privileges on *.* to \'root\'@\'%\' identified by \'root\' with grant option
    -> ;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> grant replication slave on *.* to \'backup\'@\'%\' identified by \'123456\';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show master status\\G;
*************************** 1. row ***************************
             File: mysql-bin.000001
         Position: 889
     Binlog_Do_DB: gulimall_ums,gulimall_pms,gulimall_oms,gulimall_sms,gulimall_wms,gulimall-admin
 Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)

ERROR:
No query specified

mysql> show master status
    -> ;
+------------------+----------+---------------------------------------------------------------------------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB                                                                    | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+---------------------------------------------------------------------------------+------------------+-------------------+
| mysql-bin.000001 |      889 | gulimall_ums,gulimall_pms,gulimall_oms,gulimall_sms,gulimall_wms,gulimall-admin |                  |                   |
+------------------+----------+---------------------------------------------------------------------------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> exit
Bye
root@6a7d36b510b6:/# read escape sequence
[root@database ~]#

STEP 4 从服务器开启同步

change master to master_host=\'192.168.1.117\',master_user=\'backup\', master_password=\'123456\',master_log_file=\'mysql-bin.000001\',master_log_pos=0,master_port=3307;
start slave;
show slave status;

以上是关于谷粒商城创建mysql主从(十三)的主要内容,如果未能解决你的问题,请参考以下文章

谷粒商城缓存(二十三)

谷粒商城-基础篇- 笔记四(docker安装mysql)

谷粒商城学习——P15数据库初始化

黑马Java笔记+踩坑汇总JavaSE+JavaWeb+SSM+SpringBoot+瑞吉外卖+SpringCloud/SpringCloudAlibaba+黑马旅游+谷粒商城

谷粒商城的快速开发

谷粒商城的快速开发