1. 程式人生 > 實用技巧 >在CentOS7.5中基於docker-compose安裝mysql5.7

在CentOS7.5中基於docker-compose安裝mysql5.7

一、部署規劃

伺服器IP 192.168.113.48
3306
安裝目錄 /home/work/docker-mysql-5.7
資料對映目錄 /home/work/docker-mysql-5.7/data
配置檔案 /home/work/docker-mysql-5.7/config/my.cnf

使用3306埠,安裝前請確保宿主機3306埠是否已被佔用。Mysql的預設字符集為latin1,根據配置檔案將字符集集修改為utf8mb4,該字符集支援emoji特殊字元。

二、安裝Mysql5.7

  • 在安裝目錄下新建並按照規劃編輯docker-compose.yml檔案,編輯完後儲存退出

    vim docker-compose.yml
    
    version: '3'
    services:
      mysql:
        image: mysql:5.7
        container_name: mysql-5.7
        #使用該引數,container內的root擁有真正的root許可權,否則,container內的root只是外部的一個普通使用者許可權
        #設定為true,不然資料卷可能掛載不了,啟動不起
        privileged: true
        restart: always
        ports:
          - "3306:3306"
        environment:
          MYSQL_ROOT_PASSWORD: root
          MYSQL_USER: root
          MYSQL_PASS: root
          TZ: Asia/Shanghai
        command:
          --wait_timeout=31536000
          --interactive_timeout=31536000
          --max_connections=1000
          --default-authentication-plugin=mysql_native_password
        volumes:
          #對映mysql的資料目錄到宿主機,儲存資料
          - "/home/work/docker-mysql-5.7/data:/var/lib/mysql"
          #根據宿主機下的配置檔案建立容器
          - "/home/work/docker-mysql-5.7/config/my.cnf:/etc/mysql/my.cnf"
    

    根據需要root使用者密碼可自行決定;連線時間、最大連線數可根據需要進行修改;

    --default-authentication-plugin=mysql_native_password主要解決遠端訪問問題。若MySQL為8.0版本,請修改為--default-authentication-plugin=caching_sha2_password

  • 在安裝目錄下建立config目錄,並編寫my.cnf配置檔案

    [client]
    # 客戶端來源資料的預設字符集
    default-character-set=utf8mb4
    
    [mysqld]
    # 服務端預設字符集
    character-set-server=utf8mb4
    # 連線層預設字符集
    collation-server=utf8mb4_unicode_ci
    
    [mysql]
    # 資料庫預設字符集
    default-character-set=utf8mb4
    
  • 在安裝目錄下執行相關命令,啟動容器

    docker-compose up -d
    

    檢視並進入容器當中,登入mysql,檢視字符集是否修改:

    [root@node03 docker-mysql-5.7]# docker-compose ps
      Name                 Command               State                 Ports              
    --------------------------------------------------------------------------------------
    mysql-5.7   docker-entrypoint.sh --wai ...   Up      0.0.0.0:3306->3306/tcp, 33060/tcp
    [root@node03 docker-mysql-5.7]# docker-compose exec mysql bash
    root@72f9ad3a4f36:/# mysql -uroot -proot
    mysql: [Warning] Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 35
    Server version: 5.7.31 MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
    
    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> show variables like '%character%';
    +--------------------------+----------------------------+
    | Variable_name            | Value                      |
    +--------------------------+----------------------------+
    | character_set_client     | utf8mb4                    |
    | character_set_connection | utf8mb4                    |
    | character_set_database   | utf8mb4                    |
    | character_set_filesystem | binary                     |
    | character_set_results    | utf8mb4                    |
    | character_set_server     | utf8mb4                    |
    | character_set_system     | utf8                       |
    | character_sets_dir       | /usr/share/mysql/charsets/ |
    +--------------------------+----------------------------+
    8 rows in set (0.01 sec)
    
    mysql> 
    

三、驗證遠端訪問Mysql

  • 利用工具連線資料庫