Mysql安裝
拉取鏡像缝呕,默認(rèn)都是從 DockerHub 拉取
-
docker pull mysql:5.7
拉取mysql數(shù)據(jù)庫
mysql 使用
-
啟動(dòng)mysql
MySQL 鏡像地址根據(jù)鏡像說明可知:
默認(rèn)的配置文件是:/etc/mysql/my.cnf
默認(rèn)的數(shù)據(jù)目錄是:/var/lib/mysql
- 簡單啟動(dòng)方式
docker run -p 3307:3306 --name mysql -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs:/logs -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
- 綁定配置文件啟動(dòng)
宿主機(jī)中創(chuàng)建文件夾 mysql踩麦,并分別創(chuàng)建 data 目錄和 conf 目錄协饲。新建配置文件 mysql_profile.cnf:
[mysqld]
server-id = 1 #服務(wù)Id唯一
port = 3307
log-error = /var/log/mysql/error.log
#只能用IP地址
skip_name_resolve
#數(shù)據(jù)庫默認(rèn)字符集
character-set-server = utf8mb4
#數(shù)據(jù)庫字符集對應(yīng)一些排序等規(guī)則
collation-server = utf8mb4_general_ci
#設(shè)置client連接mysql時(shí)的字符集,防止亂碼
init_connect='SET NAMES utf8mb4'
#最大連接數(shù)
max_connections = 300
接下來分別映射數(shù)據(jù)庫目錄和配置文件目錄,啟動(dòng)容器:
docker run --name mysql -d --rm \
-v /home/txl/mysql/conf:/etc/mysql/conf.d \
-v /home/txl/mysql/data:/var/lib/mysql \
-p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
配置文件解釋說明:
1矛渴、通過bash進(jìn)入啟動(dòng)的mysql container
docker exec -it mysql(容器名) bash
2蜂绎、查看my.cnf
> cat /etc/mysql/my.cnf
# Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation. The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# This program is distributed in the 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 License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
主配置文件 my.cnf 加載了 /etc/mysql/conf.d 文件夾下所有的配置(后綴必須是 .cnf),因此只需映射 conf.d 文件夾即可卷谈。
使用容器客戶端連接:
> docker exec -it mysql 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.28 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, 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>
MySQL默認(rèn)配置文件路徑:
配置文件:/etc/my.cnf
日志文件:/var/log/mysqld.log
服務(wù)啟動(dòng)腳本:/usr/lib/systemd/system/mysqld.service
socket文件:/var/run/mysqld/mysqld.pid