Linux服务器docker容器部署mysql

创建本地映射目录

[root@VM-4-16-centos data]# mkdir -p /data/mysql/{data,logs,conf}
[root@VM-4-16-centos data]#
[root@VM-4-16-centos data]# chmod  777 -R /data/mysql/logs/
[root@VM-4-16-centos data]#
[root@VM-4-16-centos data]# cd mysql/
[root@VM-4-16-centos mysql]# ls
conf  data  logs
[root@VM-4-16-centos mysql]# pwd
/data/mysql
[root@VM-4-16-centos mysql]#

创建mysql配置文件

[root@VM-4-16-centos mysql]# cd conf/
[root@VM-4-16-centos conf]# vim mysql.cnf
[root@VM-4-16-centos conf]# 
[root@VM-4-16-centos conf]# cat mysql.cnf
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
#
#skip-grant-tables
#skip-host-cache
#skip-name-resolve

#datadir=/var/lib/mysql
datadir=/data/mysql/data

#socket=/var/lib/mysql/mysql.sock
socket=/data/mysql/data/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

lower_case_table_names=1

#secure-file-priv=/var/lib/mysql-files
#user=mysql

#pid-file=/var/run/mysqld/mysqld.pid

#server-id=1
character-set-server=UTF8MB4

## 指定不需要同步的数据库名称
#binlog-ignore-db=mysql

## 开启二进制日志功能
#log-bin=mall-mysql-bin

## 设置二进制日志使用内存大小(事务)
#binlog_cache_size=1M

## 设置使用的二进制日志格式(mixed,statement,row)
#binlog_format=mixed

## 二进制日志过期清理时间。默认值为0,表示不自动清理。
#expire_logs_days=7

## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断。
## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
#slave_skip_errors=1062

[client]
socket=/data/mysql/data/mysql.sock

拉取mysq镜像

(本次以mysql5.7为例)

[root@VM-4-16-centos conf]# docker pull mysql:5.7
5.7: Pulling from library/mysql
70e9ff4420fb: Pull complete
7ca4383b183f: Pull complete
3e282e7651b1: Pull complete
1ffa0e0ca707: Pull complete
6eb790cf6382: Pull complete
b4b277ff2929: Pull complete
692fe4469429: Pull complete
c0d447d97bbd: Pull complete
99ee594517ba: Pull complete
a9ae52de4d77: Pull complete
66cc05a182b5: Pull complete
Digest: sha256:2c23f254c6b9444ecda9ba36051a9800e8934a2f5828ecc8730531db8142af83
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
[root@VM-4-16-centos conf]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               5.7                 92034fe9a41f        5 weeks ago         581MB
[root@VM-4-16-centos conf]#

创建mysql容器

[root@VM-4-16-centos conf]# docker run -d   --restart=always  --name="mysql" -p 3306:3306 -v /data/mysql/conf/:/etc/mysql/conf.d -v /data/mysql/data:/var/lib/mysql -v /data/mysql/logs:/var/log -e MYSQL_ROOT_PASSWORD="123456"  mysql:5.7
003884a5be5ca944e5346979f19834a98679ed3893d5a650025f7cab21c965f9
[root@VM-4-16-centos conf]# docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                                              NAMES
003884a5be5c        mysql:5.7             "docker-entrypoint.s…"   7 minutes ago       Up 56 seconds       0.0.0.0:3306->3306/tcp, 33060/tcp                  mysql

登录验证

[root@VM-4-16-centos conf]# docker exec -it mysql /bin/bash
bash-4.2# mysql -h 127.0.0.1 -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值