您的位置:首页 > 运维架构 > Docker

通过docker连接mysql

2016-03-12 11:52 866 查看
通过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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: