docker 安装mysql
创建mysql配置文件
创建mysql配置目录
mkdir -p /usr/local/software/mysql
创建master配置
这里master配置文件所在的文件夹3306/conf已经提前创建好了,然后是创建mysql的配置文件my.cnf
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
default_authentication_plugin=mysql_native_password
# Custom config should go here
!includedir /etc/mysql/conf.d/
server_id=200 #服务器id
log_bin=mysql-bin #日志文件
binlog_format=row #日志格式
-- 插入 --
创建运行mysql容器(master)
创建容器并运行
docker run
- -i:以交互模式运行容器
- -t:为容器重新分配一个伪输入终端
- —name :容器名称
- —privileged: 设置容器公开权限(默认为true)
- -p :映射端口 linux端口: 容器内置端口(mysql默认端口为3306)
- -v : linux挂载文件夹/文件和容器内路径的映射
- -e: 容器的环境变量(设置mysql默认用户名&密码)
- -d: 后台运行容器,并返回容器ID
docker run -it \
--name mysql_3306 \
--privileged \
-p 3306:3306 \
-v /usr/local/software/mysql/3306/conf/my.cnf:/etc/mysql/my.cnf \
-v /usr/local/software/mysql/3306/data:/var/lib/mysql \
-v /usr/local/software/mysql/3306/mysql-files:/var/lib/mysql-files \
-e MYSQL_ROOT_PASSWORD=123 \
-d mysql
查看docker运行的容器
docker ps
进入容器
进入容器实质上也是进入了一个linux环境。
docker exec
- -it:以交互模式运行容器 ,为容器重新分配一个伪输入终端。
- bash: bash(GNU Bourne-Again Shell)是最常用的一种shell(运行在终端的互动程序)。
docker exec -it mysql_3306 bash
容器中运行mysql客户端
登录mysql客户端: mysql -u用户名 -p密码
如果登录成功,则docker安装mysql成功。
退出容器,防火墙开放端口
开放指定端口
firewall-cmd —zone=public —add-port=端口号/tcp —permanent
—zone: public 公开端口
—add-port: 端口号/协议名称
—permanent: 永久开放
firewall-cmd --zone=public --add-port=3306/tcp --permanent
重新加载防火墙
firewall-cmd —reload
firewall-cmd --reload
查看开放的端口号
firewall-cmd —zone=public —list-ports
firewall-cmd --zone=public --list-ports
navicat测试连接
mysql主从配置
准备工作
检查binlog是否开启
进入mysql容器,输入命令:
show variables LIKE ‘log_%’;
ON: 表示已开启
当前mysql数据库binlog情况
登录mysql客户端: mysql -uroot -p123
使用命令: show master status;
创建与从(slave)服务通信的用户
create user 'slave'@'%' identified with mysql_native_password by '123';
GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO ‘slave’@’%’;
flush privileges;
获取master容器ip地址
docker inspect mysql_3306 |grep IPA
创建slave从服务器3310配置
创建slave配置文件my.cnf
vim /usr/local/software/mysql/3310/conf/my.cnf
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
default_authentication_plugin=mysql_native_password
# Custom config should go here
!includedir /etc/mysql/conf.d/
server_id=210
log_bin=mysql-slave01-bin
relay_log=wnhz-relay-bin
read_only=1 #设置只读
创建运行slave01容器
docker run -it \
--name mysql_3310 \
--privileged \
-p 3310:3306 \
-v /usr/local/software/mysql/3310/conf/my.cnf:/etc/mysql/my.cnf \
-v /usr/local/software/mysql/3310/data:/var/lib/mysql \
-v /usr/local/software/mysql/3310/mysql-files:/var/lib/mysql-files \
-e MYSQL_ROOT_PASSWORD=123 \
-d mysql
开放3310端口
firewall-cmd --zone=public --add-port=3310/tcp --permanent
firewall-cmd --reload
配置主从
进入slave容器,打开mysql客户端
修改从(slave)服务器与master关联
change master to
master_host='172.17.0.2',
master_user='slave',
master_password='123',
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=848;
开启slave
start slave
查询slave状态
show slave status \G;
两个yes表示配置完成
创建从服务器登录用户
create user rdb identified with mysql_native_password by '123';
grant select on *.* to rdb;
flush privileges;
测试主从
连接slave
在master创建库book_db,查看从(slave)是否也创建成功。
创建从服务器登录用户
create user rdb identified with mysql_native_password by '123';
grant select on *.* to rdb;
flush privileges;