mac 源生安装mysql_docker安装MySQL数据库
本文說明,剛上來使用MySQL高版本的,結果MySQL容器正常運行后,外部客戶端連接不上,最后降低版本就OK
拉取MySQL數據庫鏡像
# docker pull daocloud.io/library/mysql:5.7.22
[root@localhost ~]# docker pull daocloud.io/library/mysql:5.7.22Trying to pull repository daocloud.io/library/mysql ...5.7.22: Pulling from daocloud.io/library/mysql
be8881be8156: Pull complete
c3995dabd1d7: Pull complete
9931fdda3586: Pull complete
bb1b6b6eff6a: Pull complete
a65f125fa718: Pull complete
2d9f8dd09be2: Pull complete
37b912cb2afe: Pull complete
79592d21cb7f: Pull complete
00bfe968d82d: Pull complete
79cf546d4770: Pull complete
2b3c2e6bacee: Pull complete
Digest: sha256:e744510d4d03fddd1162651312afd1e591cf33b051c6f29ed64b9a3e64b97aa7
Status: Downloaded newer imagefor daocloud.io/library/mysql:5.7.22
查看拉取的鏡像文件
# docker images
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
daocloud.io/library/mysql 5.7.22 6bb891430fb6 9 months ago 372 MB
鏡像重命名
# docker tag daocloud.io/library/mysql:5.7.22 mysql:5.7.22
刪除原有鏡像名
# docker rmi daocloud.io/library/mysql:5.7.22
[root@localhost ~]# docker rmi daocloud.io/library/mysql:5.7.22Untagged: daocloud.io/library/mysql:5.7.22Untagged: daocloud.io/library/mysql@sha256:e744510d4d03fddd1162651312afd1e591cf33b051c6f29ed64b9a3e64b97aa7
查看確認
# docker images
[root@localhost ~]# docker images
mysql5.7.22 6bb891430fb6 9 months ago 372 MB
創建MySQL容器,并后臺啟動
# docker run --name djlmysql -e MYSQL_ROOT_PASSWORD=123456 -p 3307:3306 -d mysql:5.7.22
[root@localhost ~]# docker run --name djlmysql -e MYSQL_ROOT_PASSWORD=123456 -p 3307:3306 -d mysql:5.7.22fa0c44b306c1b5038d39cbe5add1dc05a9f034d21dc1e62bb52487bc8b7ff04f
[root@localhost~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fa0c44b306c1 mysql:5.7.22 "docker-entrypoint..." 22 seconds ago Up 21 seconds 0.0.0.0:3307->3306/tcp djlmysql
-e環境變量說明:
MYSQL_ROOT_PASSWORD
本變量必填,它指定了 MySQL?root?的用戶的密碼
MYSQL_DATABASE
本變量可選,通過該變量當 MySQL 啟動時會創建一個由你指定的數據庫。如果你另外又提供了一對用戶名和密碼(見下方),那么他將會被授予本數據庫的所有權限。
MYSQL_USER,?MYSQL_PASSWORD
這兩個變量可選,同時使用的話會創建一個新用戶并設置相應的密碼,該用戶會被授予由?MYSQL_DATABASE?變量指定的數據庫的所有權限(見上方)。只有當同時提供了這兩個變量時該用戶才會被創建。
特別注意沒有必要使用這個機制來創建 root 用戶,root 用戶的密碼會被設置為?MYSQL_ROOT_PASSWORD?變量的值。
MYSQL_ALLOW_EMPTY_PASSWORD
本變量可選,當其被設置為?yes?時將會允許當前容器中的 root 用戶能夠使用空密碼。注意:絕對不建議將該變量設置為?yes,除非你知道自己在做什么。如果這么做的話你的 MySQL 服務將會失去保護,所有人都可以以超級用戶的身份訪問該 MySQL 服務。
MySQL配置文件的掛載目錄
當 MySQL 服務啟動時會以?/etc/mysql/my.cnf?為配置文件,本文件會導入?/etc/mysql/conf.d?目錄中所有以?.cnf?為后綴的文件。這些文件會拓展或覆蓋?/etc/mysql/my.cnf?文件中的配置。因此你可以創建你自己需要的配置文件并掛載至 MySQL 容器中的?/etc/mysql/conf.d?目錄。
# cat my.cnf
root@70d513b30042:/etc/mysql# ls -l
total8drwxr-xr-x 2 root root 62 Jul 27 2018conf.d
lrwxrwxrwx1 root root 24 Jul 27 2018 my.cnf -> /etc/alternatives/my.cnf-rw-r--r-- 1 root root 839 Jul 9 2016my.cnf.fallback-rw-r--r-- 1 root root 796 Mar 4 2018mysql.cnf
drwxr-xr-x 2 root root 24 Jul 27 2018mysql.conf.d
root@70d513b30042:/etc/mysql# catmy.cnf
# Copyright (c)2016, Oracle and/or its affiliates. All rights reserved.
#
# This program isfree software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version2of the License.
#
# This program is distributedinthe hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public Licensefor moredetails.
#
# You should have received a copy of the GNU General Public License
# along with this program;if not, writeto the Free Software
# Foundation, Inc.,51 Franklin St, Fifth Floor, Boston, MA 02110-1301USA!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
拷貝源生MySQL配置文件到宿主機
# docker cp djlmysql:/etc/mysql/mysql.conf.d /home/mysqldata/
# docker cp djlmysql:/etc/mysql/conf.d /home/mysqldata/
[root@docker-servers ~]# docker cp djlmysql:/etc/mysql/mysql.conf.d /home/mysqldata/[root@docker-servers ~]#
[root@docker-servers ~]# ls /home/mysqldata/mysql.conf.d
[root@docker-servers ~]#
[root@docker-servers ~]#
[root@docker-servers ~]# docker cp djlmysql:/etc/mysql/conf.d /home/mysqldata/[root@docker-servers ~]#
[root@docker-servers ~]#
[root@docker-servers ~]# ls /home/mysqldata/conf.d mysql.conf.d
MySQL儲存數據的位置(可用于掛載到本地):/var/lib/mysql
# docker cp djlmysql:/var/lib/mysql /home/mysqldata/
[root@docker-servers ~]# docker cp djlmysql:/var/lib/mysql /home/mysqldata/[root@docker-servers ~]#
[root@docker-servers ~]# ls /home/mysqldata/conf.d mysql mysql.conf.d
[root@docker-servers ~]#
[root@docker-servers ~]# cd /home/mysqldata/mysql
[root@docker-servers mysql]#
[root@docker-servers mysql]# lsauto.cnf ca.pem client-key.pem ibdata1 ib_logfile1 mysql private_key.pem server-cert.pem sys
ca-key.pem client-cert.pem ib_buffer_pool ib_logfile0 ibtmp1 performance_schema public_key.pem server-key.pem
創建MySQL容器,掛載數據和配置文件到本地
-e TZ="Asia/Shanghai"
# docker run --name mysql -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -v /home/mysqldata/mysql.conf.d:/etc/mysql/mysql.conf.d -v /home/mysqldata/conf.d:/etc/mysql/conf.d -v /home/mysqldata/mysql:/var/lib/mysql -d mysql:5.7.22
[root@docker-servers ~]# docker run --name mysql -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -v /home/mysqldata/mysql.conf.d:/etc/mysql/mysql.conf.d -v /home/mysqldata/conf.d:/etc/mysql/conf.d -v /home/mysqldata/mysql:/var/lib/mysql -d mysql:5.7.229996e8b903389408070e2083abdd4811c195c5652df376ca91e91d0e5e727d87
客戶端外部連接
參考博客
https://blog.csdn.net/woniu211111/article/details/80968154
https://www.cnblogs.com/pwc1996/p/5425234.html
https://hub.daocloud.io/repos/fa51c1d6-9dc2-49d9-91ac-4bbfc24a1bda
總結
以上是生活随笔為你收集整理的mac 源生安装mysql_docker安装MySQL数据库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 新浪天气预报接口_根据新浪天气
- 下一篇: mysql55w.x86 64 卸载_C