• 容器化部署mariadb数据库


    compose文档:Compose specification | Docker Documentation

    容器地址及文档:Docker Hub

     部署环境依赖

    1.docker-ce安装

     a.卸载老版本的docker

    1. sudo yum remove docker \
    2. docker-client \
    3. docker-client-latest \
    4. docker-common \
    5. docker-latest \
    6. docker-latest-logrotate \
    7. docker-logrotate \
    8. docker-engine

    b.设置容器仓库

    1. sudo yum install -y yum-utils
    2. 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.可以通过国内镜像进行加速,本次使用的是阿里云镜像加速

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

    1. sudo mkdir -p /etc/docker
    2. sudo tee /etc/docker/daemon.json <<-'EOF'
    3. {
    4. "registry-mirrors": ["https://0cbolbbb.mirror.aliyuncs.com"]
    5. }
    6. EOF
    7. sudo systemctl daemon-reload
    8. sudo systemctl restart docker

     3.安装docker-compose

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

    参考文档编写docker-compose文件

    1.创建数据目录

    1. mkdir /data/mariadb -pv
    2. cd /data/mariadb

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

    1. cd /data/mariadb/
    2. vi docker-compose.yml
    1. version: '3.2'
    2. services:
    3. mariadb:
    4. image: bitnami/mariadb:10.7.3
    5. container_name: mariadb
    6. restart: always
    7. environment:
    8. - MARIADB_ROOT_PASSWORD=rootIPD.xx2.19
    9. - MARIADB_DATABASE=flyapps
    10. - MARIADB_USER=flyuser
    11. - MARIADB_PASSWORD=KGzKjZpWBp4R4RSa
    12. #- ALLOW_EMPTY_PASSWORD=yes
    13. - MARIADB_ENABLE_SLOW_QUERY=1
    14. - MARIADB_LONG_QUERY_TIME=3
    15. - MARIADB_SKIP_TEST_DB=yes
    16. - MARIADB_EXTRA_FLAGS=--max-connect-errors=3000 --max_connections=30000
    17. ports:
    18. - 3306:3306
    19. volumes:
    20. - /etc/localtime:/etc/localtime:ro
    21. - ./data/mariadb:/bitnami/mariadb/data
    22. - ./data/logs/mariadb:/data/logs/mariadb
    23. - ./server.cnf:/opt/bitnami/mariadb/conf/my_custom.cnf:ro

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

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

    1. cd /data/mariadb/
    2. vi server.cnf

     内容如下:

    1. #
    2. # These groups are read by MariaDB server.
    3. # Use it for options that only the server (but not clients) should see
    4. #
    5. # See the examples of server my.cnf files in /usr/share/mysql/
    6. #
    7. # this is only for the mysqld standalone daemon
    8. [mysqld]
    9. binlog_cache_size = 192K
    10. thread_stack = 384K
    11. join_buffer_size = 4096K
    12. query_cache_type = 1
    13. max_heap_table_size = 1024M
    14. default_storage_engine = InnoDB
    15. performance_schema_max_table_instances = 400
    16. table_definition_cache = 400
    17. skip-external-locking
    18. key_buffer_size = 512M
    19. max_allowed_packet = 1G
    20. table_open_cache = 1024
    21. sort_buffer_size = 2048K
    22. net_buffer_length = 4K
    23. read_buffer_size = 2048K
    24. read_rnd_buffer_size = 1024K
    25. myisam_sort_buffer_size = 16M
    26. thread_cache_size = 192
    27. query_cache_size = 256M
    28. tmp_table_size = 1024M
    29. sql-mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
    30. max_connections = 30000
    31. max_connect_errors = 1000
    32. open_files_limit = 65535
    33. expire_logs_days = 10
    34. #log_queries_not_using_indexes=on
    35. character-set-client-handshake = FALSE
    36. character-set-server = utf8mb4
    37. collation-server = utf8mb4_general_ci
    38. init_connect='SET NAMES utf8mb4'
    39. #character-set-server=utf8
    40. skip_name_resolve
    41. event_scheduler=1
    42. #skip-grant-tables
    43. #innodb_data_home_dir = /var/lib/mysql/
    44. #innodb_data_file_path = ibdata1:10M:autoextend
    45. #innodb_log_group_home_dir = /var/lib/mysql/
    46. #innodb_buffer_pool_size = 1024M
    47. #innodb_log_file_size = 128M
    48. #innodb_log_buffer_size = 32M
    49. #innodb_flush_log_at_trx_commit = 1
    50. #innodb_lock_wait_timeout = 50
    51. #innodb_max_dirty_pages_pct = 90
    52. #innodb_read_io_threads = 4
    53. #innodb_write_io_threads = 4
    54. #
    55. # Allow server to accept connections on all interfaces.
    56. #
    57. #bind-address=0.0.0.0
    58. #
    59. # this is only for embedded server
    60. # This group is only read by MariaDB servers, not by MySQL.
    61. # If you use the same .cnf file for MySQL and MariaDB,
    62. # you can put MariaDB-only options here
    63. [mariadb]
    64. #autoset_open_files_limit
    65. #enable_slow_query_log
    66. #audit
    67. plugin_load_add=server_audit
    68. server_audit_logging=on
    69. server_audit_events=connect,query
    70. server_audit=force_plus_permanent
    71. server_audit_events=QUERY_DDL,QUERY_DML,CONNECT
    72. server_audit_output_type=file
    73. server_audit_file_rotate_now=on
    74. server_audit_file_rotations=9
    75. server_audit_file_rotate_size=1G
    76. server_audit_file_path=/data/logs/mariadb
    77. log_error=/data/logs/mariadb/mariadb.err.log
    78. log_output=FILE
    79. slow_query_log
    80. long_query_time=3
    81. slow_query_log_file=/data/logs/mariadb/mariadb-slow.log
    82. log_queries_not_using_indexes=ON #Logging Queries That Don't Use Indexes
    83. #server_id=2
    84. log-bin=/data/logs/mariadb/mysql-bin
    85. # This group is only read by MariaDB-10.5 servers.
    86. # If you use the same .cnf file for MariaDB of different versions,
    87. # use this group for options that older servers don't understand

     启动容器服务

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

    1. cd /data/mariadb/
    2. mkdir -pv data/{mariadb,logs/mariadb}
    3. chown 1001.1001 -R data/{mariadb,logs/mariadb}
    1. # 根据需求进行启动
    2. #docker-compose up # 前端运行启动
    3. docker-compose up -d # 后台启动,一般执行该命令后台运行
    1. # 停止并删除容器,因为使用数据挂载,因此不会删除数据
    2. docker-compose down

    查看容器状态

    docker-compose ps

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

    1. [root@localhost mariadb]# docker-compose logs
    2. mariadb | mariadb 00:07:58.01
    3. mariadb | mariadb 00:07:58.01 Welcome to the Bitnami mariadb container
    4. mariadb | mariadb 00:07:58.01 Subscribe to project updates by watching https://github.com/bitnami/bitnami-docker-mariadb
    5. mariadb | mariadb 00:07:58.02 Submit issues and feature requests at https://github.com/bitnami/bitnami-docker-mariadb/issues
    6. mariadb | mariadb 00:07:58.02
    7. mariadb | mariadb 00:07:58.02 INFO ==> ** Starting MariaDB setup **
    8. mariadb | mariadb 00:07:58.04 INFO ==> Validating settings in MYSQL_*/MARIADB_* env vars
    9. mariadb | mariadb 00:07:58.04 INFO ==> Initializing mariadb database
    10. mariadb | mariadb 00:07:58.06 INFO ==> Updating 'my.cnf' with custom configuration
    11. mariadb | mariadb 00:07:58.06 INFO ==> Setting user option
    12. mariadb | mariadb 00:07:58.07 INFO ==> Setting slow_query_log option
    13. mariadb | mariadb 00:07:58.07 INFO ==> Setting long_query_time option
    14. mariadb | mariadb 00:07:58.08 INFO ==> Injecting custom configuration 'my_custom.cnf'
    15. mariadb | mariadb 00:07:58.08 INFO ==> Installing database
    16. mariadb | mariadb 00:07:59.16 INFO ==> Starting mariadb in background
    17. mariadb | mariadb 00:08:01.18 INFO ==> Configuring authentication
    18. mariadb | mariadb 00:08:01.26 INFO ==> Running mysql_upgrade
    19. mariadb | find: '/docker-entrypoint-startdb.d/': No such file or directory
    20. mariadb | mariadb 00:08:02.00 INFO ==> Stopping mariadb
    21. mariadb | mariadb 00:08:03.02 INFO ==> ** MariaDB setup finished! **
    22. mariadb |
    23. mariadb | mariadb 00:08:03.05 INFO ==> ** Starting MariaDB **
    24. mariadb | 2022-09-06 0:08:03 0 [Note] /opt/bitnami/mariadb/sbin/mysqld (server 10.7.3-MariaDB-log) starting as process 1 ...
    25. mariadb | mariadb 00:08:31.76
    26. mariadb | mariadb 00:08:31.76 Welcome to the Bitnami mariadb container
    27. mariadb | mariadb 00:08:31.76 Subscribe to project updates by watching https://github.com/bitnami/bitnami-docker-mariadb
    28. mariadb | mariadb 00:08:31.76 Submit issues and feature requests at https://github.com/bitnami/bitnami-docker-mariadb/issues
    29. mariadb | mariadb 00:08:31.76
    30. mariadb | mariadb 00:08:31.77 INFO ==> ** Starting MariaDB setup **
    31. mariadb | mariadb 00:08:31.79 INFO ==> Validating settings in MYSQL_*/MARIADB_* env vars
    32. mariadb | mariadb 00:08:31.79 INFO ==> Initializing mariadb database
    33. mariadb | mariadb 00:08:31.80 INFO ==> Updating 'my.cnf' with custom configuration
    34. mariadb | mariadb 00:08:31.81 INFO ==> Setting user option
    35. mariadb | mariadb 00:08:31.81 INFO ==> Setting slow_query_log option
    36. mariadb | mariadb 00:08:31.81 INFO ==> Setting long_query_time option
    37. mariadb | mariadb 00:08:31.82 INFO ==> Injecting custom configuration 'my_custom.cnf'
    38. mariadb | mariadb 00:08:31.82 INFO ==> Using persisted data
    39. mariadb | mariadb 00:08:31.84 INFO ==> Running mysql_upgrade
    40. mariadb | mariadb 00:08:31.85 INFO ==> Starting mariadb in background
    41. mariadb | find: '/docker-entrypoint-startdb.d/': No such file or directory
    42. mariadb | mariadb 00:08:34.56 INFO ==> Stopping mariadb
    43. mariadb | mariadb 00:08:35.58 INFO ==> ** MariaDB setup finished! **
    44. mariadb |
    45. mariadb | mariadb 00:08:35.60 INFO ==> ** Starting MariaDB **
    46. 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

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

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

    systemctl enable docker

  • 相关阅读:
    5-1:什么是Servlet-开发你的第一个动态网站
    【C++】用命名空间避免命名冲突
    Windows 下 MSVC 编译器在 CMake 生成时提示 RC failed 或库文件缺失
    5个节约生命的python小技巧~~~
    NB15 牛群编号的回文顺序II
    【负荷预测】基于改进灰狼算法(IGWO)优化的LSSVM进行负荷预测(Matlab代码实现)
    InnoDB存储引擎存储结构详解-实战篇
    Bot代码的执行(微服务)
    Linux网络编程- ioctl()结合struct ifreq使用案例
    ES6...
  • 原文地址:https://blog.csdn.net/ly1358152944/article/details/126721806