Windows VMWare Centos环境下安装Docker并配置MySql
虚拟机安装
官网下载Centos Stream 10系统镜像
安装了Minimal版,Terminal中粘贴、复制指令不方便,又新建了虚拟机,安装GUI版
终端输入指令报错修复
输入指令报错:failed to set locale defaulting to C.UTF-8,安装语言包
# 查看语言包
$ locale -a# 安装英文语言包
$ sudo dnf install glibc-langpack-en
安装Docker
#使用YUM包管理器
sudo yum update
sudo dnf -y install yum-utils#安装 dnf-plugins-core包(提供管理 DNF 仓库的命令)
sudo dnf -y install dnf-plugins-core#设置镜像sudo yum-config-manager --add-repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo#安装最新版本的 Docker-CE 和 containerd 执行如下命令
sudo yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin#安装完成后启动docker服务
sudo systemctl start docker#检查docker是否正常运行
sudo systemctl status docker#设置docker自动启动
sudo systemctl enable docker
检查网络联通
VMWare 中Centos的网络信息:
windows宿主机,ping 虚拟机:
Docker 安装Mysql
初次拉取报错Error response from daemon。
duel@localhost:~$ docker pull mysql:5.7.25
permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post "http://%2Fvar%2Frun%2Fdocker.sock/v1.51/images/create?fromImage=docker.io%2Flibrary%2Fmysql&tag=5.7.25": dial unix /var/run/docker.sock: connect: permission denied
duel@localhost:~$ sudo docker pull mysql:5.7.25
Error response from daemon: Get "https://registry-1.docker.io/v2/": net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
解决方法:通过vim在daemon.json中添加镜像加速。
sudo vim /etc/docker/daemon.json
{"registry-mirrors": ["https://docker.hpcloud.cloud","https://docker.m.daocloud.io","https://docker.unsee.tech","https://docker.1panel.live","http://mirrors.ustc.edu.cn","https://docker.chenby.cn","http://mirror.azure.cn","https://dockerpull.org","https://dockerhub.icu","https://hub.rat.dev"]
}
vim编辑保存后,重启docker,并再次尝试拉取MySql:
sudo systemctl daemon-reload
sudo systemctl restart docker
sudo systemctl status docker
sudo docker pull mysql:5.7.25
拉取MySql成功后,创建文件夹做挂载
$ sudo mkdir /mydata/mysql/conf
$ sudo mkdir /mydata/mysql/data
$ sudo mkdir /mydata/mysql/conf
创建my.cnf配置文件,并在vim中添加内容
#创建my.cnf配置文件
sudo touch /mydata/mysql/conf/my.cnf#vim中打开
sudo vim /mydata/mysql/conf/my.cnf
vim中添加的内容如下:
[mysqld]
user=mysql
character-set-server=utf8
default_authentication_plugin=mysql_native_password
secure_file_priv=/var/lib/mysql
expire_logs_days=7
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
max_connections=1000[client]
default-character-set=utf8[mysql]
default-character-set=utf8
创建并运行
# sudo docker run --restart=always --privileged=true -p 3306:3306 --name mysql -v /mydata/mysql/log:/var/log/mysql -v /mydata/mysql/data:/var/lib/mysql -v /mydata/mysql/conf/my.cnf:/etc/mysql/my.cnf -v /mydata/mysql/conf/conf.d:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=pwd123456 -d mysql:5.7.25sudo docker run --restart=always --privileged=true -p 3306:3306 --name mysql
-v /mydata/mysql/log:/var/log/mysql
-v /mydata/mysql/data:/var/lib/mysql
-v /mydata/mysql/conf/my.cnf:/etc/mysql/my.cnf
-v /mydata/mysql/conf/conf.d:/etc/mysql/conf.d
-e MYSQL_ROOT_PASSWORD=pwd123456
-d mysql:5.7.25
参数说明
--restart=always: 当Docker 重启时,容器会自动启动。
--privileged=true:容器内的root拥有真正root权限,否则容器内root只是外部普通用户权限
-p 3306:3306:将容器的3306端口映射到主机的3306端口
-v /mydata/mysql/conf:/etc/mysql:将配置文件夹挂载到主机
-v /mydata/mysql/log:/var/log/mysql:将日志文件夹挂载到主机
-v /mydata/mysql/data:/var/lib/mysql:将配置文件夹挂载到主机
-v /mydata/mysql/conf/my.cnf:/etc/mysql/my.cnf:映射配置文件
-v /mydata/mysql/conf/conf.d:/etc/mysql/conf.d:映射配置文件
-e MYSQL_ROOT_PASSWORD=pwd123456:初始化root用户的密码
-d mysql:5.7.25 以后台方式启动
#进入容器
sudo docker exec -it mysql bash
root@96f90204aefb:/# mysql -u root -p
Enter password: pwd123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.25 MySQL Community Server (GPL)Copyright (c) 2000, 2019, 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> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
mysql> select user,host from user;
+---------------+-----------+
| user | host |
+---------------+-----------+
| root | % |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-----------+
4 rows in set (0.00 sec)
windows宿主机中使用Navicat测试连接:
mysql> exit
Bye
root@96f90204aefb:/# exit
exit
duel@localhost:~$ sudo shutdown -h now
重启虚拟机后,查看Docker状态,测试MySql自启成功:
duel@localhost:~$ sudo systemctl status dockerduel@localhost:~$ sudo docker exec -it mysql bash
root@96f90204aefb:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.25 MySQL Community Server (GPL)Copyright (c) 2000, 2019, 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>