安装docker镜像时,先去docker hub上查找对应的官方镜像,然后选择版本,然后根据描述汇中的命令来启动镜像
docker hub官网地址:https://hub.docker.com/
docker pull 镜像名称:镜像版本(不写默认latest)
docker images
docker ps
docker ps -a
docker exec -it 容器名称(或容器id) /bin/bash
exit
docker logs -f 容器名称(或容器id)
docker rmi 镜像id(或镜像名称:标签)
docker rmi -f 镜像id(或镜像名称:标签)
docker rm 容器id或容器名称
docker rm -f 容器id或容器名称
docker save -o 导出后的名称.tar 镜像标识
docker load < 导入的文件
docker update --restart=always 容器标识
docker build -t runoob/ubuntu:v1 .
docker commit {containerId} {image:tag}
下载:docker pull mysql:5.7
镜像启动:
docker run --name mysql5.7 \
-v /data/mysql5.7/data:/var/lib/mysql \
-v /data/mysql5.7/conf:/etc/mysql/conf.d \
-v /data/mysql5.7/log:/var/log/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
-p 3310:3306 -d mysql:5.7
命令注释:
docker run --name mysql5.7 \ #容器名称
-v /data/mysql5.7/data:/var/lib/mysql \ #挂载数据目录
-v /data/mysql5.7/conf:/etc/mysql/conf.d \ #挂载配置目录
-v /data/mysql5.7/log:/var/log/mysql \ #挂载日志目录
-e MYSQL_ROOT_PASSWORD=123456 \ #设置ROOT初始化密码
-p 3310:3306 -d mysql:5.7 #-p 端口映射 -d后台启动 镜像名称:标签
docker pull mysql:8.0
docker run --name mysql \
-v /data/mysql/data:/var/lib/mysql \
-v /data/mysql/conf:/etc/mysql/conf.d \
-v /data/mysql/log:/var/log/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
-p 3306:3306 -d \
mysql:8.0
docker ps -a
在目录/data/mysql/conf
上创建mysql.cnf
配置文件
[mysqld]
#设置服务器id,为1表示主服务器,实例唯一ID,不能和canal的slaveId重复
server_id=1
#启动MySQ二进制日志系统
log-bin=mysql-bin
#选择row模式
binlog-format=ROW
#需要同步的数据库名,如果有多个数据库,可重复此参数,每个数据库一行
binlog-do-db=xxl_job
#不同步mysql系统数据库
binlog-ignore-db=mysql
在目录/data/mysql2/conf
上创建mysql.cnf
配置文件
[mysqld]中配置如下
#实例唯一ID,不能和canal的slaveId重复,表示为从数据库
server-id=2
#启动MySQL二进制日志系统
log-bin=mysql-bin
#选择row模式
binlog-format=ROW
#需要同步的数据库名,如果有多个数据库,可重复此参数,每个数据库一行
replicate-do-db=xxl_job
#不同步mysql系统数据库
replicate-ignore-db=mysql
在目录/data/mysql3/conf
上创建mysql.cnf
配置文件
[mysqld]中配置如下
#实例唯一ID,不能和canal的slaveId重复,表示为从数据库
server-id=3
#启动MySQL二进制日志系统
log-bin=mysql-bin
#选择row模式
binlog-format=ROW
#需要同步的数据库名,如果有多个数据库,可重复此参数,每个数据库一行
replicate-do-db=xxl_job
#不同步mysql系统数据库
replicate-ignore-db=mysql
接着上面启动mysql第二个容器和第三个容器,并重启第一个容器
重启mysql容器:docker restart mysql
镜像启动mysql2容器:
docker run --name mysql2 \
-v /data/mysql2/data:/var/lib/mysql \
-v /data/mysql2/conf:/etc/mysql/conf.d \
-v /data/mysql2/log:/var/log/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
-p 3307:3306 -d \
mysql:8.0
镜像启动mysql3容器:
docker run --name mysql3 \
-v /data/mysql3/data:/var/lib/mysql \
-v /data/mysql3/conf:/etc/mysql/conf.d \
-v /data/mysql3/log:/var/log/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
-p 3308:3306 -d \
mysql:8.0
在主数据库中执行 show master status;
在从库中执行以下命令:
CHANGE MASTER TO
MASTER_HOST='服务器主机地址',
MASTER_USER='用户名',
MASTER_PASSWORD='密码!',
MASTER_LOG_FILE='二进制文件名',
MASTER_LOG_POS=位置;
我这边对应的命令是:
CHANGE MASTER TO
MASTER_HOST='192.168.73.168',
MASTER_USER='root',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000002',
MASTER_LOG_POS=156;
如果执行报下面的错误,需要先通过命令stop slave;
停一下之前启动是slave
> 3021 - This operation cannot be performed with a running slave io thread; run STOP SLAVE IO_THREAD FOR CHANNEL '' first.
通过命令start slave;
开启主从同步
通过命令show slave status;
查看开启同步状态是否成功
slave_io_running 和 slave_sql_running 都是 yes 代表成功
不成功的,请检查以下几项
最后就是主从复制检验了,主库数据变更看是否自动同步到从库
引入依赖
<dependency>
<groupId>org.apache.shardingspheregroupId>
<artifactId>sharding-jdbc-spring-boot-starterartifactId>
<version>4.0.0-RC1version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.1.14version>
dependency>
application.properties中的配置
# shardingjdbc 配置
spring.shardingsphere.datasource.names=master,slave1,slave2
# 主数据源配置
spring.shardingsphere.datasource.master.type=org.apache.tomcat.jdbc.pool.DataSource
spring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master.url=jdbc:mysql://192.168.73.168:3306/xxl_job?Unicode=true&characterEncoding=UTF-8
spring.shardingsphere.datasource.master.username=root
spring.shardingsphere.datasource.master.password=123456
# 从数据源配置1
spring.shardingsphere.datasource.slave1.type=org.apache.tomcat.jdbc.pool.DataSource
spring.shardingsphere.datasource.slave1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.slave1.url=jdbc:mysql://192.168.73.168:3307/xxl_job?Unicode=true&characterEncoding=UTF-8
spring.shardingsphere.datasource.slave1.username=root
spring.shardingsphere.datasource.slave1.password=123456
# 从数据源配置2
spring.shardingsphere.datasource.slave2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.slave2.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.slave2.url=jdbc:mysql://192.168.73.168:3308/xxl_job?Unicode=true&characterEncoding=UTF-8
spring.shardingsphere.datasource.slave2.username=root
spring.shardingsphere.datasource.slave2.password=123456
#配置默认数据源master 默认数据源,主要用于写
spring.shardingsphere.sharding.default-data-source-name=master
## 读写分离配置
spring.shardingsphere.masterslave.load-balance-algorithm-type=round_robin
## 最终的数据源名称
spring.shardingsphere.masterslave.name=dataSource
## 主库数据源名称
spring.shardingsphere.masterslave.master-data-source-name=master
## 从库数据源名称列表,多个逗号分隔
spring.shardingsphere.masterslave.slave-data-source-names=slave1,slave2
## 开启SQL显示
spring.shardingsphere.props.sql.show=true
拉取:docker pull elasticsearch:7.16.3
启动:
docker run -d --name elasticsearch
-p 9200:9200 -p 9300:9300
-e "discovery.type=single-node"
-v /data/elasticsearch/data:/usr/share/elasticsearch/data
-v /data/elasticsearch/config:/usr/share/elasticsearch/config
elasticsearch:7.16.3
命令注释:
docker run -d --name elasticsearch # -d 后台启动 --name 容器名称
-p 9200:9200 -p 9300:9300 # -p 端口映射
-e "discovery.type=single-node" # 启动参数:单节点启动
-v /data/elasticsearch/data:/usr/share/elasticsearch/data # 挂载数据目录
-v /data/elasticsearch/config:/usr/share/elasticsearch/config # 挂载配置目录
elasticsearch:7.16.3 # 镜像名称:镜像标签
拉取:docker pull logstash:7.16.3
启动:
docker run -d \
--name=logstash \
--restart=always \
-p 5044:5044 \
-v /data/logstash/data/:/usr/share/logstash/data/ \
-v /data/logstash/config/:/usr/share/logstash/config/ \
-v /data/logstash/logs/:/usr/share/logstash/logs/ \
logstash:7.16.3
mkdir -p /data/rocketmq/rmqserver01/logs
mkdir -p /data/rocketmq/rmqserver01/store
mkdir -p /data/rocketmq/rmqbroker01/logs
mkdir -p /data/rocketmq/rmqbroker01/store
mkdir -p /data/rocketmq/rmqbroker01/conf
#创建server容器
docker create -p 9876:9876 --name rmqserver01
-e "JAVA_OPT_EXT=-server -Xms128m -Xmx128m -Xmn128m"
-e "JAVA_OPTS=-Duser.home=/opt"
-v /data/rocketmq/rmqserver01/logs:/opt/logs
-v /data/rocketmq/rmqserver01/store:/opt/store
foxiswho/rocketmq:server-4.3.2
#启动容器
docker start rmqserver rmqbroker
#启动broker容器
docker run -it -d --net host --name rmqbroker01 \
-e "JAVA_OPT_EXT=-server -Xms128m -Xmx128m -Xmn128m" \
-e "JAVA_OPTS=-Duser.home=/opt" \
-v /data/rocketmq/rmqbroker01/conf/broker.conf:/etc/rocketmq/broker.conf \
-v /data/rocketmq/rmqbroker01/logs:/opt/logs \
-v /data/rocketmq/rmqbroker01/store:/opt/store \
--privileged=true \
foxiswho/rocketmq:broker-4.3.2
#启动rocketmq-console
docker run -d \
-e "JAVA_OPTS=-Drocketmq.namesrv.addr=192.168.73.168:9876;192.168.73.168:9877 -Dcom.rocketmq.sendMessageWithVIPChannel=false" \
-p 8082:8080 -t \
styletang/rocketmq-console-ng:1.0.0