Docker下簡單使用MySQL

  • Docker安裝
>curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

OK

>sudo add-apt-repository \

"deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \

$(lsb_release -cs) \

stable"

>sudo apt-get install docker-ce

>sudo vi /etc/default/docker

DOCKER_OPTS="--registry-mirror=https://48h46wxj.mirror.aliyuncs.com"

>sudo usermod -aG docker username

>sudo service docker restart
  • MySQL鏡像及容器建立

下載源GPG祕鑰html

>curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

>sudo docker pull mysql (Using default tag:latest )
  1. 啓動第一個mysql服務
>sudo docker run -d -p 3307:3306 --name mysql3306 \

-v /opt/docker_v/mysql/3306/conf/3306.cnf:/etc/mysql/my.cnf \

-v /opt/docker_v/mysql/3306/data:/var/lib/mysql/data/3306 \

-v -e MYSQL_ROOT_PASSWORD=123456 \

mysql:latest

 

    2. 啓動第二個mysql服務mysql

>sudo docker run -d -p 127.0.0.1:3308:3306 --name mysql3307 \

-v /opt/docker_v/mysql/3307/conf/3306.cnf:/etc/mysql/my.cnf \

-v /opt/docker_v/mysql/3307/data:/var/lib/mysql \

-e MYSQL_ROOT_PASSWORD=123456 \

mysql:latest

 

    3. 宿主機訪問Docker容器:linux

$ mysql -h127.0.0.1 -P3308 -uroot

 

    4. Docker MySQL 配置示例redis

# 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

#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

 

    5. MySQL 主從複製sql

mysql-master-server:

IP:172.17.0.2]

my.cnf

[mysqld]

... 其餘配置

server-id=1

log-bin=mysql-bin #

mysql-slave-server:

IP:172.17.0.3

my.cnf

[mysqld]

... 其餘配置

server-id=2

mysql> change master to master_host='172.17.0.2', master_user='root', master_password='123456';

mysql> start slave;

mysql> show slave status\G;
相關文章
相關標籤/搜索