通过docker连接mysql

原创 2016年03月12日 11:52:58
通过docker连接mysql:
cs202@cs202-devbox:~$ sudo docker run -it --link mysql --rm mysql sh -c 'exec mysql -h "172.17.0.3" -P3306 -uroot -pwwwwww'
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.11 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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 databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)
mysql> 
docker是轻量级的,可自由使用的(disposable),所以最好对于特定的任务使用单一的容器,而不是在一个容器里进行2种任务。
So you're learning how to use Docker and you want to install Wordpress, Drupal or any project that needs MySQL? Well, you've come to the right place. Docker can be hard sometimes, and when you add complexity to something that it's already hard (at least for beginners), you end up drinking more coffee than your body can take.
The thing about Docker is that you can run any service, like MySQL or Apache inside a container. You can run more than one service inside the container but it's not a good practice. You must also know that containers are pretty much disposable, and that you can run multiple containers on a machine, each one doing just one thing at a time. 
所以对于mysql来说,我们可以使用一个容器提供Mysql server服务,另一个容器作为客户端使用。
MySQL as you might know is a database server, when you run the MySQL server process, you need to get into MySQL using the MySQL client and create a database, a user, grant permissions, etc. How to do this while using Docker? Well, you need to create a Docker container running MySQL server and then create another container running MySQL client that can connect to the container running MySQL Server.
Before I continue let me explain images. Images are like templates for your containers, there's hundreds of images for pretty much anything you can imagine. With an image everything is installed and configured for you so you can create one or many containers using the same image. You can also create your own images, but I recommend that you go step by step.
In this case we'll be using this MySQL image. Remember, with the same image, we'll be running two containers, one with MySQL server running and another one with MySQL client talking to the container running the server.
The command to run MySQL server is the following:
启动作为Mysql server使用的容器:
docker run --name some-mysql -e MYSQLROOTPASSWORD=mysecretpassword -d mysql
Let's explain each part of that command:
docker is the command so there's not too much to say about it
run is the first argument and is used to tell docker that we want to run a container
--name some-mysql is the name argument following the name of the container. This is not really neccesary but it's very helpful. Since we can use the same image to run multiple containers of the same image, we need to name them. Docker is smart enough to name the containers for us in case we don't name it, but the names are random.
-e The "-e" part is just telling docker to pass and environment variable to the container with a name and a value. With environment variables we pass information to the container that the container will use to do something. In this case to set the root password.
MYSQLROOTPASSWORD=mysecretpassword is the environment variable name and the value of the variable. You can pass any variable that you want, some containers need a variable to do something and are waiting for it. Others don't need any variables, it really depends on the image and what you want to do with the variable inside the container. In this case, as I mentioned earlier, we use them to set automatically the root password.
-d The "-d" argument is just to tell docker that we want to daemonize the container and leave it running until it dies or until we kill it.
mysql is the name of the image and if it doesn't exists on our computer docker will pull it from the docker public repository.
Ok that part was easy, if you run that command you'll have a docker container running MySQL server. But how do we connect to it? We still need to create a database. Well, you can connect to the container using the following command:
启动mysql客户端连接的容器(注意$MYSQLPORT3306TCPADDR,$MYSQLPORT3306TCPPORT,$MYSQLENVMYSQLROOTPASSWORD要使用实际的IP,PORT,PASSWORD代替):
查看容器的IP,PORT等信息,可以通过inscept,如docker inspect container_name:
cs202@cs202-devbox:~$ sudo docker inspect mysql
[
{
    "Id": "b50461bd813558074e19bbfcc40de41c5c771092ea97d664bc524af7145fa669",
    "Created": "2016-03-12T02:50:08.820418406Z",
    "Path": "/entrypoint.sh",
    "Args": [
        "mysqld"
    ],
    "State": {
        "Status": "running",
        "Running": true,
        "Paused": false,
        "Restarting": false,
        "OOMKilled": false,
        "Dead": false,
        "Pid": 5299,
        "ExitCode": 0,
        "Error": "",
        "StartedAt": "2016-03-12T02:50:09.339103967Z",
        "FinishedAt": "0001-01-01T00:00:00Z"
    },
    ....
    Gateway": "172.17.0.1",
        "GlobalIPv6Address": "",
        "GlobalIPv6PrefixLen": 0,
        "IPAddress": "172.17.0.3",
        "IPPrefixLen": 16,
        "IPv6Gateway": "",
        "MacAddress": "02:42:ac:11:00:03",
        "Networks": {
            "bridge": {
                "EndpointID": "8d652bbe4a3952d69fe9413d4b119a207b2099fab0b61c14a34f4598b3f1a76f",
                "Gateway": "172.17.0.1",
                "IPAddress": "172.17.0.3",
                "IPPrefixLen": 16,
                "IPv6Gateway": "",
                "GlobalIPv6Address": "",
                "GlobalIPv6PrefixLen": 0,
                "MacAddress": "02:42:ac:11:00:03"
            }
        }
            ....
docker run -it --link some-mysql:mysql --rm mysql sh -c 'exec mysql -h"$MYSQLPORT3306TCPADDR" -P"$MYSQLPORT3306TCPPORT" -uroot -p"$MYSQLENVMYSQLROOTPASSWORD"'
连接成功:
cs202@cs202-devbox:~$ sudo docker run -it --link mysql --rm mysql sh -c 'exec mysql -h "172.17.0.3" -P3306 -uroot -pwwwwww'
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.11 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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 databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)
可以随意做一些操作:
mysql> create database test1;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test1              |
+--------------------+
5 rows in set (0.00 sec)
mysql> use test1;
Database changed
mysql> create table a( a int);
Query OK, 0 rows affected (0.02 sec)
mysql> insert into a values(1);
Query OK, 1 row affected (0.02 sec)
mysql> select * from a;
+------+
| a    |
+------+
|    1 |
+------+
1 row in set (0.00 sec)
mysql> 
That's a little bit more complicated but all we're doing is using the same mysql image to create an interactive container and then run the command "exec mysql" with a bunch of arguments and environment variables.
每一次连接Mysql都需要敲那么长的一串命令吗,答案是YES,但是有更好的办法,作者写了4个脚本可以帮助更好的利用docker容器中的mysql进行工作。
You might be asking right now if you need to enter that command every time you want to connect to MySQL server. The answer is yes, but there's a better way.
Last night, I created Docker-MySQL-Scripts, a collection of 4 scripts written in python to helps you interact with a dockerized MySQL.
You can download them at https://github.com/luiselizondo/docker-mysql-scripts
dmysql-server
Replaces the first command I explained earlier. With it you can run a MySQL container really easy. All you have to do is pass the container name and the root password:
dmysql-server myappdb 123
In this case, the name of the container will be "myappdb" and the root password will be 123. Easy right?
dmysql-client
Replaces the second command I mentioned earlier. With it you can run MySQL client and connect to a container running MySQL. All you have to do is pass the name of the container running MySQL Server that you want to connect to:
dmysql myappdb
In this case, it will connect to the MySQL server running on the container named "myappdb"
dmysql-create-database
The name says what it does, it will create a database inside the container. All you have to do is pass the MySQL container name you want to connect to and the name of the database you want to create.
dmysql-create-database myappdb myblog
In this case it will connect to the container "myappdb" and issue the command CREATE DATABASE myblog
dmysql-import-database
Again, the name says what it does, it will take a file and import it to a database. This is a little more complicated than the rest of the commands but it's easier than using a docker command. You have to pass the name of the container, the SQL file you want to import (right now it only accepts *.sql files so you'll have to ungzip them first) and the database you want to import the file into. The database is optional since the sql file can create one for you.
dmysql-import-database myappdb /Users/me/myblog-monday-backup.sql --database myblog
In this case, it will import the file myblog-monday-backup.sql into the database myblog running on the container myappdb.
That's it. Using those simple commands you can save yourself hours of frustration. If you need help or if you have an idea please leave a comment or even better, you can fork the project and submit a pull request.
Update 10/29/2014
The latest version of the official MySQL image now supports creating a Database user with a password and a database if the environment variables are passed. The scripts are still working but now you have another option to create a database.

Docker实战(十三):Docker安装MySQL数据库

Docker安装MySQL数据库
  • birdben
  • birdben
  • 2016年06月19日 15:20
  • 30722

Docker安装mysql 配置远程登录

一、前言 目前的虚拟机上已经安装过mysql服务了,最近正好在学习Docker,于是就想用docker另外整一套环境出来,先从mysql开始吧。 二、过程 1.docker search mysq...
  • smartdt
  • smartdt
  • 2017年09月28日 17:37
  • 470

使用docker构建简单的本地的mysql服务

博客原文 安利一篇我翻译的国外大牛的神经网络入门文章 环境 liujinliu@liujinliu-OptiPlex-7010:~$ uname -a Linux liujinliu-O...
  • github_25679381
  • github_25679381
  • 2017年01月07日 18:26
  • 929

docker如何简单部署mysql

#1    docker pull mysql #2    docker run -v /data/var/mysql/:/var/lib/mysql -e MYSQL_ROOT_PASSWOR...
  • ownfire
  • ownfire
  • 2015年12月12日 13:40
  • 20222

docker mysql 主从配置

1、首先创建两个文件my-m.cnf(主库配置) 、my-s.cnf(从库配置) my-m.cnf 内容如下# Copyright (c) 2014, Oracle and/or its affil...
  • qq362228416
  • qq362228416
  • 2015年09月19日 04:27
  • 5563

docker启动Mysql

cs202@cs202-devbox:~$ sudo docker run -d mysql 7698fdd7a2d05d38cf19c60cc9e35c1117fc551ae5e319144947...
  • zhou1862324
  • zhou1862324
  • 2016年03月12日 10:57
  • 10895

Docker创建MySQL容器

Docker创建MySQL容器
  • kongxx
  • kongxx
  • 2014年08月15日 08:09
  • 49462

使用docker 搭建基础的 mysql 应用

mysql 服务器是目前比较流行的开源数据库服务器,下面介绍如何使用docker来做一个mysql数据库服务从网站直接 pull 一个 mysql 的镜像core@localhost ~/base $...
  • qcpm1983
  • qcpm1983
  • 2014年10月14日 17:11
  • 8278

在docker中使用mysql数据库,在局域网访问

开发过程中经常需要安装、调试mysql数据库,还需要在各种操作系上安装包依赖,实在是繁琐,因此就研究了一下如何在docker上运行一个mysql镜像,省却了我安装、找依赖的问题。 注:本文所有内容均在...
  • minicto
  • minicto
  • 2017年06月20日 11:15
  • 5106

Docker运行Mysql实例

使用docker运行mysql实例 Docker MYsql镜像运用
  • xiaolyuh123
  • xiaolyuh123
  • 2017年05月22日 19:35
  • 1921
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:通过docker连接mysql
举报原因:
原因补充:

(最多只允许输入30个字)