温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

docker中如何安装mysql

发布时间:2021-11-19 11:30:58 来源:亿速云 阅读:157 作者:小新 栏目:开发技术

这篇文章主要介绍docker中如何安装mysql,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

    1、docker hub上面查找mysql镜像

[root@t-docker tomcatlogs]# docker search mysql
NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                                                  MySQL is a widely used, open-source relation…   6845                [OK]                
mariadb                                                MariaDB is a community-developed fork of MyS…   2185                [OK]                
mysql/mysql-server                                     Optimized MySQL Server Docker images. Create…   502                                     [OK]
percona                                                Percona Server is a fork of the MySQL relati…   365                 [OK]                
zabbix/zabbix-server-mysql                             Zabbix Server with MySQL database support       120

    2、从docker hub上(或阿里云加速器)拉取mysql镜像到本地

[root@t-docker tomcatlogs]# docker pull mysql:5.6
5.6: Pulling from library/mysql
be8881be8156: Already exists 
c3995dabd1d7: Pulling fs layer 
9931fdda3586: Pulling fs layer 
bb1b6b6eff6a: Pulling fs layer 
a65f125fa718: Pulling fs layer 
62fa8db7a5dc: Pulling fs layer 
a65f125fa718: Waiting 
0f5681d76128: Pull complete 
56d3348c5742: Pull complete 
b93f67de42c4: Pull complete 
5adba6c10127: Pull complete 
Digest: sha256:2e48836690b8416e4890c369aa174fc1f73c125363d94d99cfd08115f4513ec9
Status: Downloaded newer image for mysql:5.6
[root@t-docker tomcatlogs]# docker images mysql
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               5.6                 7edb93321b06        4 weeks ago         256MB

    3、使用mysql镜像创建容器(也叫运行镜像)

[root@t-docker chenzx]# docker run -p 12345:3306 --name mysql -v /volume/mysql/conf:/etc/mysql/conf.d -v /volume/mysql/logs:/logs -v /volume/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6

命令说明:

-p 12345:3306:将主机的12345端口映射到docker容器的3306端口;

--name mysql:运行容器的名字

-v 挂载宿主机上的数据卷到容器里面

-e MYSQL_ROOT_PASSWORD=123456:初始化root的密码

-d mysql:5.6:后台运行mysql5.6

[root@t-docker chenzx]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                              NAMES
6dc80df5e339        mysql:5.6           "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:12345->3306/tcp            mysql
[root@t-docker chenzx]# docker exec -it 6dc80df5e339 /bin/bash
root@6dc80df5e339:/# mysql -uroot -p123456
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 1
Server version: 5.6.41 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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> status ;
--------------
mysql  Ver 14.14 Distrib 5.6.41, for Linux (x86_64) using  EditLine wrapper
Connection id:1
Current database:
Current user:root@localhost
SSL:Not in use
Current pager:stdout
Using outfile:''
Using delimiter:;
Server version:5.6.41 MySQL Community Server (GPL)
Protocol version:10
Connection:Localhost via UNIX socket
Server characterset:latin1
Db     characterset:latin1
Client characterset:latin1
Conn.  characterset:latin1
UNIX socket:/var/run/mysqld/mysqld.sock
Uptime:5 min 10 sec
Threads: 1  Questions: 5  Slow queries: 0  Opens: 67  Flush tables: 1  Open tables: 60  Queries per second avg: 0.016
--------------

    4、备份docker容器里面的MySQL

[root@t-docker chenzx]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                              NAMES
6dc80df5e339        mysql:5.6           "docker-entrypoint.s…"   16 minutes ago      Up 16 minutes       0.0.0.0:12345->3306/tcp            mysql
[root@t-docker chenzx]# docker exec 6dc80df5e339 sh -c 'exec mysqldump -uroot -p123456 -B mysql' > mysql.sql

以上是“docker中如何安装mysql”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI