【Docker】 安装 mysql
生活随笔
收集整理的這篇文章主要介紹了
【Docker】 安装 mysql
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
目錄
- 前言
- 準(zhǔn)備
- 拉取鏡像
- 創(chuàng)建但不運(yùn)行容器
- 運(yùn)行容器
- 命令行登錄mysql
- 修改密碼
- mysql 隨 docker 自動(dòng)啟動(dòng)
- 設(shè)置mysql數(shù)據(jù)編碼格式為utf8mb4
- 設(shè)置mysql表名不區(qū)分大小寫(xiě)
- 重啟mysql并查看設(shè)置
- 遠(yuǎn)程訪問(wèn)授權(quán)
- 參考
前言
- CentOS 7.9
- Docker Version: 1.13.1
準(zhǔn)備
-
docker 已安裝。
-
選擇合適的mysql鏡像(mysql:5.7.31)。
拉取鏡像
shell> docker pull mysql:5.7.31 Trying to pull repository docker.io/library/mysql ... 5.7.31: Pulling from docker.io/library/mysql bb79b6b2107f: Pull complete 49e22f6fb9f7: Pull complete 842b1255668c: Pull complete 9f48d1f43000: Pull complete c693f0615bce: Pull complete 8a621b9dbed2: Pull complete 0807d32aef13: Pull complete 6d2fc69dfa35: Pull complete 56153548dd2c: Pull complete 3bb6ba940303: Pull complete 3e1888da91a7: Pull complete Digest: sha256:b3dc8d10307ab7b9ca1a7981b1601a67e176408be618fc4216d137be37dae10b Status: Downloaded newer image for docker.io/mysql:5.7.31 shell> docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/hello-world latest feb5d9fea6a5 6 weeks ago 13.3 kB docker.io/mysql 5.7.31 42cdba9f1b08 12 months ago 448 MB創(chuàng)建但不運(yùn)行容器
shell> docker create --restart=always --name mysql1 \ -v /data/mysql1/data:/var/lib/mysql \ -v /data/mysql1/conf.d:/etc/mysql/conf.d \ -e MYSQL_ROOT_PASSWORD=my-secret-pw mysql:5.7.31- --restart=always: 自動(dòng)運(yùn)行
- -v /data/mysql1/data:/var/lib/mysql: 將宿主機(jī)的/data/mysql1/data掛載成容器的/var/lib/mysql
- -v /data/mysql1/conf.d:/etc/mysql/conf.d: 將宿主機(jī)的/data/mysql1/conf.d掛載成容器的/etc/mysql/conf.d
運(yùn)行容器
shell> docker start mysql1命令行登錄mysql
shell> docker exec -it mysql1 bash root@a6f9e4a700a3:/# mysql -uroot -pmy-secret-pw 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 2 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> exit Bye或者:
shell> docker exec -it mysql1 mysql -uroot -pmy-secret-pw 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 2 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> exit Bye修改密碼
mysql> set password=password('123456');mysql 隨 docker 自動(dòng)啟動(dòng)
創(chuàng)建容器未添加--restart=always參數(shù)時(shí),可以使用docker update添加。
shell> docker update --restart=always mysql1- 每次docker啟動(dòng)時(shí),mysql也會(huì)自動(dòng)啟動(dòng)
設(shè)置mysql數(shù)據(jù)編碼格式為utf8mb4
shell> vim /data/mysql1/conf.d/my.cnf # 打開(kāi)文件(沒(méi)有該文件時(shí),創(chuàng)建它)后,添加下面的配置。注意對(duì)應(yīng)節(jié)點(diǎn) [client] default-character-set = utf8mb4[mysql] default-character-set = utf8mb4[mysqld] character-set-client-handshake = FALSE character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci init_connect = 'SET NAMES utf8mb4'設(shè)置mysql表名不區(qū)分大小寫(xiě)
shell> vim /data/mysql1/conf.d/my.cnf ----------------------------- # 打開(kāi)文件后,添加下面的配置。注意對(duì)應(yīng)節(jié)點(diǎn) [mysqld] lower_case_table_names = 1重啟mysql并查看設(shè)置
shell> docker exec -it mysql1 mysql -uroot -p123456 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 2 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 WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%'; +--------------------------+--------------------+ | 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 | | collation_connection | utf8mb4_unicode_ci | | collation_database | utf8mb4_unicode_ci | | collation_server | utf8mb4_unicode_ci | +--------------------------+--------------------+ 10 rows in set (0.01 sec)mysql> show variables like '%table_names'; +------------------------+-------+ | Variable_name | Value | +------------------------+-------+ | lower_case_table_names | 1 | +------------------------+-------+ 1 row in set (0.00 sec)mysql> exit Bye遠(yuǎn)程訪問(wèn)授權(quán)
參考這里。
參考
https://hub.docker.com/_/mysql
總結(jié)
以上是生活随笔為你收集整理的【Docker】 安装 mysql的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 一文帮你解决全屋WiFi覆盖问题全屋覆盖
- 下一篇: AX5400路由器怎么设置tp5400路