docker安装MySQL
2021/7/4 19:21:43
本文主要是介绍docker安装MySQL,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
以下为操作步骤为docker中安装MySQL,亲测有效
环境:以下步骤都是基于 阿里云centos8 docker containerd.io-1.4.6-3.1.el7.x86_64.rpm
一、首先安装docker
sudo yum install -y yum-utils device-mapper-persistent-data lvm2 dnf install https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.4.6-3.1.el7.x86_64.rpm sudo yum install docker-ce docker-ce-cli containerd.io sudo yum install docker-ce docker-ce-cli
注:在安装过程中,会提示 y/N 输入 y即可,每个步骤操作过程中最后提示complete,即操作成功。
安装完成之后输入以下命令,检查docker是否安装成功
sudo systemctl start docker docker --version 输出以下内容表示成功安装: [root@i1eoZ ~]# docker --version Docker version 20.10.7, build f0df350
出现docker的版本信息,则安装成功。
接着设置docker开机启动:
sudo systemctl enable docker systemctl start docker.service
接着输入命令:
[root@i1eoZ ~]# docker info 输出表示docker已经正常运行: Client: Context: default Debug Mode: false Plugins: app: Docker App (Docker Inc., v0.9.1-beta3) buildx: Build with BuildKit (Docker Inc., v0.5.1-docker) scan: Docker Scan (Docker Inc., v0.8.0) ......
2、安装MySQL
查看docker中运行的哪些镜像
[root@if1eoZ ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 以上输出表示目前还没镜像
输入docker search mysql
[root@iZ1eoZ ~]# docker search mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 11082 [OK] mariadb MariaDB Server is a high performing open sou… 4199 [OK] mysql/mysql-server Optimized MySQL Server Docker images. Create… 823 [OK] mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 88 centos/mysql-57-centos7 MySQL 5.7 SQL database server 88 centurylink/mysql Image containing mysql. Optimized to be link… 59 [OK] databack/mysql-backup Back up mysql databases to... anywhere! 44 deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK] prom/mysqld-exporter 39 [OK] tutum/mysql Base docker image to run a MySQL database se… 35 linuxserver/mysql A Mysql container, brought to you by LinuxSe… 30 schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 29 [OK] mysql/mysql-router MySQL Router provides transparent routing be… 21 centos/mysql-56-centos7 MySQL 5.6 SQL database server 20 arey/mysql-client Run a MySQL client from a docker container 17 [OK] fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas… 14 [OK] yloeffler/mysql-backup This image runs mysqldump to backup data usi… 7 [OK] genschsa/mysql-employees MySQL Employee Sample Database 7 [OK] openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image… 6 devilbox/mysql Retagged MySQL, MariaDB and PerconaDB offici… 3 cytopia/mysql-5.5 MySQL 5.5 on CentOS 7 3 [OK] ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 2 [OK] widdpim/mysql-client Dockerized MySQL Client (5.7) including Curl… 1 [OK] jelastic/mysql An image of the MySQL database server mainta… 1 monasca/mysql-init A minimal decoupled init container for mysql 0
接着拉去MySQL镜像
[root@iZ1eoZ ~]# docker pull mysql Using default tag: latest latest: Pulling from library/mysql b4d181a07f80: Pull complete a462b60610f5: Pull complete 578fafb77ab8: Pull complete 524046006037: Pull complete d0cbe54c8855: Pull complete aa18e05cc46d: Pull complete 32ca814c833f: Pull complete 9ecc8abdb7f5: Pull complete ad042b682e0f: Pull complete 71d327c6bb78: Pull complete 165d1d10a3fa: Pull complete 2f40c47d0626: Pull complete 后续省略....
查看镜像:docker images
[root@iZoZ ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql latest 5c62e459e087 10 days ago 556MB
在opt下创建文件夹
cd /opt/
mkdir mysql_docker
cd mysql_docker/
echo $PWD
[root@i1eoZ ~]# cd /opt/ [root@i1eoZ opt]# ls containerd [root@ioZ opt]# mkdir mysql_docker [root@iZoZ opt]# ls containerd mysql_docker [root@i1eoZ opt]# cd mysql_docker/ [root@itf1eoZ mysql_docker]# pwd /opt/mysql_docker [root@iZ1eoZ mysql_docker]# echo $PWD /opt/mysql_docker
启动mysql容器,在var/lib/docker/containers/下查看容器
[root@iZ21eoZ mysql_docker]# docker run --name mysqlserver -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs:/logs -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d -i -p 3306:3306 mysql:latest a9b7a794c25d8780ff773155e0acb0eb2d170a60a08573272a25c9742829d621 [root@itf1eoZ mysql_docker]# ls conf data logs [root@iZ2f1eoZ mysql_docker]# cd /var/lib/docker/containers/ [root@iZf1eoZ containers]# ls a9b7a794c25d8780ff773155e0acb0eb2d170a60a08573272a25c9742829d621 [root@iZ2z1eoZ containers]# cd /opt/mysql_docker/ [root@iZf1eoZ mysql_docker]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a9b7a794c25d mysql:latest "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysqlserver
7、进入mysql容器,并登陆mysql
命令:docker exec -it mysqlserver bash
命令:mysql -uroot -p
[root@iZeoZ mysql_docker]# docker exec -it mysqlserver bash root@a9b7a794c25d:/# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.25 MySQL Community Server - GPL Copyright (c) 2000, 2021, Oracle and/or its affiliates. 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> select now(); +---------------------+ | now() | +---------------------+ | 2021-07-03 15:38:02 | +---------------------+ 1 row in set (0.00 sec)
开启远程访问权限
命令:use mysql;
命令:select host,user from user;
命令:ALTER USER ‘root’@’%’ IDENTIFIED WITH mysql_native_password BY ‘123456’;
命令:grant all privileges on . to ‘root’@’%’ ;
命令:flush privileges;
注释:
如果需要添加新的用户,用以下命令:
CREATE USER ‘newroot’@’%’ IDENTIFIED WITH mysql_native_password BY ‘123456’;
mysql> use mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select host,user from user; +-----------+------------------+ | host | user | +-----------+------------------+ | % | root | | localhost | mysql.infoschema | | localhost | mysql.session | | localhost | mysql.sys | | localhost | root | +-----------+------------------+ 5 rows in set (0.00 sec) mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.01 sec)
查询远程连接的用户:
mysql> select host,user,plugin,authentication_string from mysql.user; +-----------+------------------+-----------------------+------------------------------------------------------------------------+ | host | user | plugin | authentication_string | +-----------+------------------+-----------------------+------------------------------------------------------------------------+ | % | root | mysql_native_password | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | | localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | localhost | mysql.session | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | localhost | mysql.sys | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | ]u|MNV">fkCOyLyrg.9AvdBkjzlOHTH72OOvK0xakKg21y2TwgWleF6 |$A$005$ +-----------+------------------+-----------------------+------------------------------------------------------------------------+ 5 rows in set (0.00 sec) 注释:如果有新添加的用户,表里面也会显示出来
另外,在安装过程中还遇到一个问题,就是在docker镜像里面不能用ifconfig查看IP地址,解决方法:
yum install net-tools 如果输入这个命令报错的话,用下面这个命令: apt-get install net-tools 输入后可能会报下面这个错误 root@a9b7a794c25d:/# apt-get install net-tools Reading package lists... Done Building dependency tree Reading state information... Done E: Unable to locate package net-tools 这时候我们输入以下命令: root@a9b7a794c25d:/# apt-get update Get:1 http://security.debian.org/debian-security buster/updates InRelease [65.4 kB] Get:2 http://deb.debian.org/debian buster InRelease [122 kB] Get:3 http://deb.debian.org/debian buster-updates InRelease [51.9 kB] Get:4 http://deb.debian.org/debian buster/main amd64 Packages [7907 kB] Get:5 http://security.debian.org/debian-security buster/updates/main amd64 Packages [293 kB] Get:6 http://repo.mysql.com/apt/debian buster InRelease [21.5 kB] Get:7 http://repo.mysql.com/apt/debian buster/mysql-8.0 amd64 Packages [6017 B] Get:8 http://deb.debian.org/debian buster-updates/main amd64 Packages [15.2 kB] Fetched 8481 kB in 3min 14s (43.6 kB/s) Reading package lists... Done
接着输入命令:apt-get install net-tools
root@a9b7a794c25d:/# apt-get install net-tools Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: net-tools 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 248 kB of archives. After this operation, 1002 kB of additional disk space will be used. Get:1 http://deb.debian.org/debian buster/main amd64 net-tools amd64 1.60+git20180626.aebd88e-1 [248 kB] Fetched 248 kB in 2s (116 kB/s) debconf: delaying package configuration, since apt-utils is not installed Selecting previously unselected package net-tools. (Reading database ... 9233 files and directories currently installed.) Preparing to unpack .../net-tools_1.60+git20180626.aebd88e-1_amd64.deb ... Unpacking net-tools (1.60+git20180626.aebd88e-1) ... Setting up net-tools (1.60+git20180626.aebd88e-1) ...
然后输入ifconfig就可以正常使用了。
遗留问题:
本来想用Navicat,或者DataGrip来连接docker中的mysql,遇到了一些问题,还没解决,后续解决了会同步,或者有已经解决的欢迎留言,相互学习,谢谢!
这篇关于docker安装MySQL的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-20部署MySQL集群入门:新手必读指南
- 2024-11-20部署MySQL集群教程:初学者指南
- 2024-11-20部署MySQL集群项目实战:新手教程
- 2024-11-20部署MySQL集群资料:新手入门教程
- 2024-11-20MySQL集群部署教程:入门级详解
- 2024-11-20MySQL集群教程:入门与实践指南
- 2024-11-20部署MySQL集群教程:新手入门指南
- 2024-11-20MySQL读写分离教程:轻松入门
- 2024-11-20部署MySQL集群入门:一步一步搭建你的数据库集群
- 2024-11-19部署MySQL集群学习:入门教程