先定义一个 Dockerfile,我们可以从这个 Dockerfile 中构建一个自定义的镜像,从而获取到一个我们自定义的容器。
FROM centos:7
RUN yum install -y \
vim bash-com* openssh-clients openssh-server iproute cronie;\
yum group install -y "Development Tools";yum clean all;\
localedef -c -f UTF-8 -i zh_CN zh_CN.UTF-8 && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENV LANG=zh_CN.UTF-8
在上面的 Dockerfile 中,安装的基本的软件,和 sshd、crond 服务。
会支持 ssh 远程连接、计划任务、ip 命令 ,ss 命令, 还有开发工具。
version: '3.8'
services:
slb:
build: .
image: centos7-sshd
privileged: true
command: /usr/sbin/init
hostname: slb
networks:
xiuyun_net:
tomcat1:
image: centos7-sshd
privileged: true
hostname: tomcat1.sharkyun.com
command: /usr/sbin/init
networks:
xiuyun_net:
tomcat2:
image: centos7-sshd
hostname: tomcat2.sharkyun.com
privileged: true
command: /usr/sbin/init
networks:
xiuyun_net:
mysql-master:
image: centos7-sshd
hostname: mysql-master.sharkyun.com
privileged: true
command: /usr/sbin/init
networks:
xiuyun_net:
mysql-slave:
image: centos7-sshd
hostname: mysql-slave.sharkyun.com
privileged: true
command: /usr/sbin/init
networks:
xiuyun_net:
ansible:
image: centos7-sshd
hostname: ansible.sharkyun.com
privileged: true
command: /usr/sbin/init
networks:
xiuyun_net:
networks:
xiuyun_net:
docker-compose up -d
在 ansible 主机上执行
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum install ansible -y
在 ansible 主机上修改 文件 /etc/ansible/ansible.cfg 中如下选项
[defaults]
host_key_checking = False
继续在 ansible 主机上执行如下命令,创造自己的密钥对
ssh-keygen -N '' -f ~/.ssh/id_rsa
在 ansible 主机上创建文件 hosts.ini ,并写入如下内容
[nginx]
slb
[tomcat]
tomcat1
tomcat2
[mysql]
mysql-master
mysql-slave
由于这里是使用容器作为虚拟机的,所以默认情况下,root 用户没有密码,需要我们手动设置。
在宿主机上执行如下命令设置密码
docker-compose exec 服务名称 bash -c 'echo 密码 | passwd root --stdin'
docker-compose exec slb bash -c 'echo upsa | passwd root --stdin'
docker-compose exec tomcat1 bash -c 'echo upsa | passwd root --stdin'
docker-compose exec tomcat2 bash -c 'echo upsa | passwd root --stdin'
docker-compose exec mysql-master bash -c 'echo upsa | passwd root --stdin'
docker-compose exec mysql-slave bash -c 'echo upsa | passwd root --stdin'
以下操作在 ansible 主机上操作
send-pubkey.yml
---
- hosts: all
gather_facts: no
remote_user: root
vars:
ansible_ssh_pass: upsa
tasks:
- name: Set authorized key taken from file
authorized_key:
user: root
state: present
key: "{
{ lookup('file', '/root/.ssh/id_rsa.pub') }}"
...
执行 playbook
ansible-playbook -i hosts.ini send-pubkey.yml
mysql/install-mysql.yml
---
- name: 安装 mysql,并启动服务
hosts: mysql
gather_facts: no
tasks:
- name: 安装仓库文件
yum:
name: https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
state: present
- name: 禁用 MySQL 8.0 仓库
ini_file:
path: /etc/yum.repos.d/mysql-community.repo
section: mysql80-community
option: enabled
value: '0'
- name: 启用 MySQL 5.7 仓库
ini_file:
path: /etc/yum.repos.d/mysql-community.repo
section: mysql57-community
option: enabled
value: '1'
- name: 安装 mysql 社区版
yum:
name: mysql-community-server
state: present
- name: start mysql
service:
name: mysqld
state: started
...
执行 playbook
ansible-playbook -i hosts.ini mysql/install-mysql.yml
找到密码
grep password /var/log/mysqld.log
重新设置密码
mysqladmin -uroot -p'找到的初始密码' password '新密码'
创库: db
create database db default charset utf8mb4 collate utf8mb4_general_ci;
创表: stu
create table db.stu(
id int auto_increment primary key,
name varchar(4),
age int,
phone char(11));
insert into stu(name,age,phone)
values('王麻子',32,'13141098899'),
('九筒',32,'13721096541'),
('六子',18,'13139192657'),
('马邦德',37,'15855996688');
查询出 年龄大于 18 的学员姓名
统计出年龄 大于等于 32 的学员数量
统计出每个年龄段的学员数量–>需要用到分组查询
统计出 年龄在 30 到 50 之间的学员信息(id,name,age,phone)
查出年龄是 32 岁,并且电话号是13721096541 的学员姓名
mysql> select name from db.stu where age > 18;
+-----------+
| name |
+-----------+
| 王麻子 |
| 九筒 |
| 马邦德 |
+-----------+
3 rows in set (0.00 sec)
mysql> select count(name) from db.stu where age >= 32;
+-------------+
| count(name) |
+-------------+
| 3 |
+-------------+
1 row in set (0.00 sec)
mysql> select count(name) 数量 from db.stu where age >= 32;
+--------+
| 数量 |
+--------+
| 3 |
+--------+
1 row in set (0.00 sec)
mysql> select age 年龄,count(age) 数量 from db.stu group by age;
+--------+--------+
| 年龄 | 数量 |
+--------+--------+
| 18 | 1 |
| 32 | 2 |
| 37 | 1 |
+--------+--------+
3 rows in set (0.00 sec)
假如希望按照数量进行排序
mysql> select age 年龄,count(age) 数量 from db.stu group by age order by 数量;
+--------+--------+
| 年龄 | 数量 |
+--------+--------+
| 18 | 1 |
| 37 | 1 |
| 32 | 2 |
+--------+--------+
3 rows in set (0.00 sec)
mysql> select id,name,name,age,phone from db.stu where age between 30 a
nd 50;
+----+-----------+-----------+------+-------------+
| id | name | name | age | phone |
+----+-----------+-----------+------+-------------+
| 1 | 王麻子 | 王麻子 | 32 | 13141098899 |
| 2 | 九筒 | 九筒 | 32 | 13721096541 |
| 4 | 马邦德 | 马邦德 | 37 | 15855996688 |
+----+-----------+-----------+------+-------------+
3 rows in set (0.00 sec)
mysql> select name from db.stu where age=32 and phone='13721096541';
+--------+
| name |
+--------+
| 九筒 |
+--------+
1 row in set (0.00 sec)
在主服务上操作,设置二进制日志文件信息并开启二进制日志
log-bin=/var/log/mysql/mysql-bin
server-id=1
innodb_flush_log_at_trx_commit = 1
sync_binlog = 1
mkdir /var/log/mysql/
chown mysql.mysql /var/log/mysql
systemctl restart mysqld
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' IDENTIFIED BY '密码';
mysqldump -uroot -p'密码' --all-databases --master-data=1 > dbdump.db
MySQL 的主服务器上执行
scp dbdump.db root@mysql-slave:/root/
在从服务器上操作
假如 从服务器的 root 初始密码没有修改,需要按照上面 master 提到的方法修改一下
在从服务器 /root/ 目录下执行下面的命令
mysql -uroot -p'密码' < dbdump.db
编辑 /etc/my.cnf
文件,并添加如下内容
[mysqld]
server-id=2
systemctl restart mysqld
先查看二进制文件的同步信息
[root@mysql-master ~]# grep '^CHANGE' dbdump.db
CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS=437;
注意这里的二进制文件名: mysql-bin.000003
位置: 437
登录到 从库设置连接到主机的信息
CHANGE MASTER TO
MASTER_HOST='mysql-master',
MASTER_USER='repl',
MASTER_PASSWORD='密码',
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_AUTO_POSITION=1;
mysql> start slave;
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: mysql-master
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 437
Relay_Log_File: mysql-slave-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
这样是查看 IO 进程和 SQL 进程是否都是 OK
Slave_IO_Running: Yes
Slave_SQL_Running: Yes