Docker安裝MySql-掛載外部數據和配置


環境

  • CentOS:7
  • Docker:1.31.1
  • MySql:5.7
 
拷貝mysql配置文件

1.首先創建mysql容器
sudo docker run --name mysql5.7 -p 3306:3306 -e MYSQL\_ROOT\_PASSWORD=123456 -d mysql:5.7
 
2.創建成功,查看一下運行狀態
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
c84a366e3abf        mysql:5.7           "docker-entrypoint..."   4 minutes ago       Up 15 seconds       33060/tcp, 0.0.0.0:4306->3306/tcp   mysql5.7
 
3.可以看到我們的容器正在運行中,現在進入容器,查看一下配置文件
[root@localhost ~]# docker exec -it mysql5.7 /bin/bash
root@c84a366e3abf:/# 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 as published by
# the Free Software Foundation; version 2 of the License.
#
# 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 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/
root@c84a366e3abf:/# cat /etc/mysql/mysql.conf.d/mysqld.cnf
# Copyright (c) 2014, 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 as published by
# the Free Software Foundation; version 2 of the License.
#
# 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 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

#
# The MySQL  Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
lower_case_table_names=1
#log-error      = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address   = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
 
4.把輸出的內容復制一下,我們在本地先建一個同名文件,把內容粘貼進去,此時也可以修改添加自己想要的配置,例如加上忽略表名大小寫的配置:lower_case_table_names=1。
 
5.把這個文件保存一下,然后切換到命令行,輸入exit退出容器。
 
6.下一步停止並刪除mysql5.7容器,后面掛載了外部數據的時候重新創建。
[root@localhost ~]# docker stop mysql5.7
[root@localhost ~]# docker rm mysql5.7
 
數據和配置掛載到宿主機

1.在linux環境下創建掛載目錄,把前面拷出來的配置復制到下面創建的mysqld.cnf中
[root@localhost ~]# cd /opt
[root@localhost ~]# mkdir mysql
[root@localhost ~]# cd mysql
[root@localhost ~]# mkdir data
[root@localhost ~]# mkdir config
[root@localhost ~]# cd config
[root@localhost ~]# touch mysqld.cnf
 
2.mysqld.cnf的配置內容如下
[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
lower_case_table_names=1
#log-error      = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address   = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
 
3.執行下面的命令創建容器
sudo docker run --name mysql5.7 --restart always --privileged=true -p 4306:3306 -v /opt/mysql/config/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf -v /opt/mysql/data:/var/lib/mysql -e MYSQL_USER="fengwei" -e MYSQL_PASSWORD="pwd123" -e MYSQL_ROOT_PASSWORD="rootpwd123" -d mysql:5.7
參數說明:
–restart always:開機啟動
–privileged=true:提升容器內權限
-v /opt/mysql/config/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf:映射配置文件
-v /opt/mysql/data:/var/lib/mysql:映射數據目錄
-e MYSQL_USER=”fengwei”:添加用戶fengwei
-e MYSQL_PASSWORD=”pwd123”:設置fengwei的密碼偉pwd123
-e MYSQL_ROOT_PASSWORD=”rootpwd123”:設置root的密碼偉rootpwd123
特別說明:如果沒有添加--privileged=true參數,容器創建后不能正常啟動,查看日志發現有權限的錯誤。
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM