容器化部署mariadb数据库

compose文档:Compose specification | Docker Documentation

容器地址及文档:Docker Hub

 部署环境依赖

1.docker-ce安装

 a.卸载老版本的docker

sudo yum remove docker \
                docker-client \
                docker-client-latest \
                docker-common \
                docker-latest \
                docker-latest-logrotate \
                docker-logrotate \
                docker-engine

b.设置容器仓库

sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

c.安装容器引擎,默认安装最新版本

sudo yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin

d.启动容器

systemctl start docker

2.可以通过国内镜像进行加速,本次使用的是阿里云镜像加速

阿里云容器加速访问网址:阿里云登录 - 欢迎登录阿里云,安全稳定的云计算服务平台 

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://0cbolbbb.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

 3.安装docker-compose

curl -L https://get.daocloud.io/docker/compose/releases/download/v2.10.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

chmod +x /usr/local/bin/docker-compose

参考文档编写docker-compose文件

1.创建数据目录

mkdir /data/mariadb -pv
cd /data/mariadb

2.创建 docker-compose.yml文件,内容如下

cd /data/mariadb/
vi docker-compose.yml
version: '3.2'


services:

  mariadb:
    image: bitnami/mariadb:10.7.3
    container_name: mariadb 
    restart: always
    environment:
            - MARIADB_ROOT_PASSWORD=rootIPD.xx2.19
            - MARIADB_DATABASE=flyapps
            - MARIADB_USER=flyuser
            - MARIADB_PASSWORD=KGzKjZpWBp4R4RSa
              #- ALLOW_EMPTY_PASSWORD=yes
            - MARIADB_ENABLE_SLOW_QUERY=1
            - MARIADB_LONG_QUERY_TIME=3
            - MARIADB_SKIP_TEST_DB=yes
            - MARIADB_EXTRA_FLAGS=--max-connect-errors=3000 --max_connections=30000
    ports:
      - 3306:3306

    volumes:
       - /etc/localtime:/etc/localtime:ro
       - ./data/mariadb:/bitnami/mariadb/data
       - ./data/logs/mariadb:/data/logs/mariadb
       - ./server.cnf:/opt/bitnami/mariadb/conf/my_custom.cnf:ro

注意: mariadb环境变量设置,请参考官方文档Docker Hub

3.数据库配置文件 server.cnf

cd /data/mariadb/
vi server.cnf

 内容如下:

#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#


# this is only for the mysqld standalone daemon
[mysqld]
binlog_cache_size = 192K
thread_stack = 384K
join_buffer_size = 4096K
query_cache_type = 1
max_heap_table_size = 1024M

default_storage_engine = InnoDB
performance_schema_max_table_instances = 400
table_definition_cache = 400
skip-external-locking
key_buffer_size = 512M
max_allowed_packet = 1G
table_open_cache = 1024
sort_buffer_size = 2048K
net_buffer_length = 4K
read_buffer_size = 2048K
read_rnd_buffer_size = 1024K
myisam_sort_buffer_size = 16M
thread_cache_size = 192
query_cache_size = 256M
tmp_table_size = 1024M
sql-mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES


max_connections = 30000
max_connect_errors = 1000
open_files_limit = 65535

expire_logs_days = 10
#log_queries_not_using_indexes=on

character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
init_connect='SET NAMES utf8mb4'
#character-set-server=utf8
skip_name_resolve
event_scheduler=1
#skip-grant-tables


#innodb_data_home_dir = /var/lib/mysql/
#innodb_data_file_path = ibdata1:10M:autoextend
#innodb_log_group_home_dir = /var/lib/mysql/
#innodb_buffer_pool_size = 1024M
#innodb_log_file_size = 128M
#innodb_log_buffer_size = 32M
#innodb_flush_log_at_trx_commit = 1
#innodb_lock_wait_timeout = 50
#innodb_max_dirty_pages_pct = 90
#innodb_read_io_threads = 4
#innodb_write_io_threads = 4



#
# Allow server to accept connections on all interfaces.
#
#bind-address=0.0.0.0
#
# this is only for embedded server
# This group is only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb]
#autoset_open_files_limit
#enable_slow_query_log

#audit
plugin_load_add=server_audit
server_audit_logging=on
server_audit_events=connect,query
server_audit=force_plus_permanent
server_audit_events=QUERY_DDL,QUERY_DML,CONNECT
server_audit_output_type=file
server_audit_file_rotate_now=on
server_audit_file_rotations=9
server_audit_file_rotate_size=1G
server_audit_file_path=/data/logs/mariadb


log_error=/data/logs/mariadb/mariadb.err.log

log_output=FILE
slow_query_log
long_query_time=3
slow_query_log_file=/data/logs/mariadb/mariadb-slow.log
log_queries_not_using_indexes=ON  #Logging Queries That Don't Use Indexes



#server_id=2
log-bin=/data/logs/mariadb/mysql-bin

# This group is only read by MariaDB-10.5 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for options that older servers don't understand

 启动容器服务

需要提前创建好容器所需目录和授权

cd /data/mariadb/
mkdir -pv data/{mariadb,logs/mariadb}
chown 1001.1001 -R data/{mariadb,logs/mariadb}
# 根据需求进行启动
#docker-compose up   # 前端运行启动

docker-compose up -d # 后台启动,一般执行该命令后台运行
# 停止并删除容器,因为使用数据挂载,因此不会删除数据
docker-compose down

查看容器状态

docker-compose ps

 查看容器运行日志 docker-compose logs

[root@localhost mariadb]# docker-compose logs
mariadb  | mariadb 00:07:58.01 
mariadb  | mariadb 00:07:58.01 Welcome to the Bitnami mariadb container
mariadb  | mariadb 00:07:58.01 Subscribe to project updates by watching https://github.com/bitnami/bitnami-docker-mariadb
mariadb  | mariadb 00:07:58.02 Submit issues and feature requests at https://github.com/bitnami/bitnami-docker-mariadb/issues
mariadb  | mariadb 00:07:58.02 
mariadb  | mariadb 00:07:58.02 INFO  ==> ** Starting MariaDB setup **
mariadb  | mariadb 00:07:58.04 INFO  ==> Validating settings in MYSQL_*/MARIADB_* env vars
mariadb  | mariadb 00:07:58.04 INFO  ==> Initializing mariadb database
mariadb  | mariadb 00:07:58.06 INFO  ==> Updating 'my.cnf' with custom configuration
mariadb  | mariadb 00:07:58.06 INFO  ==> Setting user option
mariadb  | mariadb 00:07:58.07 INFO  ==> Setting slow_query_log option
mariadb  | mariadb 00:07:58.07 INFO  ==> Setting long_query_time option
mariadb  | mariadb 00:07:58.08 INFO  ==> Injecting custom configuration 'my_custom.cnf'
mariadb  | mariadb 00:07:58.08 INFO  ==> Installing database
mariadb  | mariadb 00:07:59.16 INFO  ==> Starting mariadb in background
mariadb  | mariadb 00:08:01.18 INFO  ==> Configuring authentication
mariadb  | mariadb 00:08:01.26 INFO  ==> Running mysql_upgrade
mariadb  | find: '/docker-entrypoint-startdb.d/': No such file or directory
mariadb  | mariadb 00:08:02.00 INFO  ==> Stopping mariadb
mariadb  | mariadb 00:08:03.02 INFO  ==> ** MariaDB setup finished! **
mariadb  | 
mariadb  | mariadb 00:08:03.05 INFO  ==> ** Starting MariaDB **
mariadb  | 2022-09-06  0:08:03 0 [Note] /opt/bitnami/mariadb/sbin/mysqld (server 10.7.3-MariaDB-log) starting as process 1 ...
mariadb  | mariadb 00:08:31.76 
mariadb  | mariadb 00:08:31.76 Welcome to the Bitnami mariadb container
mariadb  | mariadb 00:08:31.76 Subscribe to project updates by watching https://github.com/bitnami/bitnami-docker-mariadb
mariadb  | mariadb 00:08:31.76 Submit issues and feature requests at https://github.com/bitnami/bitnami-docker-mariadb/issues
mariadb  | mariadb 00:08:31.76 
mariadb  | mariadb 00:08:31.77 INFO  ==> ** Starting MariaDB setup **
mariadb  | mariadb 00:08:31.79 INFO  ==> Validating settings in MYSQL_*/MARIADB_* env vars
mariadb  | mariadb 00:08:31.79 INFO  ==> Initializing mariadb database
mariadb  | mariadb 00:08:31.80 INFO  ==> Updating 'my.cnf' with custom configuration
mariadb  | mariadb 00:08:31.81 INFO  ==> Setting user option
mariadb  | mariadb 00:08:31.81 INFO  ==> Setting slow_query_log option
mariadb  | mariadb 00:08:31.81 INFO  ==> Setting long_query_time option
mariadb  | mariadb 00:08:31.82 INFO  ==> Injecting custom configuration 'my_custom.cnf'
mariadb  | mariadb 00:08:31.82 INFO  ==> Using persisted data
mariadb  | mariadb 00:08:31.84 INFO  ==> Running mysql_upgrade
mariadb  | mariadb 00:08:31.85 INFO  ==> Starting mariadb in background
mariadb  | find: '/docker-entrypoint-startdb.d/': No such file or directory
mariadb  | mariadb 00:08:34.56 INFO  ==> Stopping mariadb
mariadb  | mariadb 00:08:35.58 INFO  ==> ** MariaDB setup finished! **
mariadb  | 
mariadb  | mariadb 00:08:35.60 INFO  ==> ** Starting MariaDB **
mariadb  | 2022-09-06  0:08:35 0 [Note] /opt/bitnami/mariadb/sbin/mysqld (server 10.7.3-MariaDB-log) starting as process 1 ...

查看mariadb服务日志,需要打开挂载目录进行查看

cd /data/mariadb/data/logs/mariadb

连接测试,输入配置文件内指定的用户

容器化部署mariadb数据库_第1张图片

 若有配置服务开机启动,需要设置docker服务开机启动即可

systemctl enable docker

你可能感兴趣的:(技能,linux,Linux,tools)