1.5.1 在Win10上准备centos7

  1. 和大家说明一下,我们的目的仅仅是要安装一个centos7,然后在centos7上安装docker
  2. 如果搞不定vagrant+virtualbox的方式,也可以直接使用VM搭建一个centos7
  3. 或者你可以直接使用一台云服务器,上面安装了centos7
  4. 毕竟我们的目的只是为了得到一个centos7的机器,所以不必花太多精力在这个问题上折腾
  5. 我上课用的环境是
  6. win10 64
  7. VirtualBox-6.0.12-133076-Win [已上传到网盘的“上课课件/virtualbox”目录]
  8. vagrant_2.2.6_x86_64 [已上传到网盘的“上课课件/vagrant”目录]
  9. centos7 [已上传到网盘的“上课课件”目录]
  10. XShell6

采坑指南:如果安装过程碰到一些问题,我特地给大家准备了一份手记,放在gper上

https://gper.club/articles/7e7e7f7ff7g58gc1g6e

采用vagrant+virtual box

1.5.1.1 下载安装vagrant

  1. 01 访问Vagrant官网
  2. https://www.vagrantup.com/
  3. 02 点击Download
  4. WindowsMacOSLinux
  5. 03 选择对应的版本
  6. 04 傻瓜式安装
  7. 05 命令行输入vagrant,测试是否安装成功

1.5.1.2 下载安装virtual box

  1. 01 访问VirtualBox官网
  2. https://www.virtualbox.org/
  3. 02 选择左侧的“Downloads
  4. 03 选择对应的操作系统版本
  5. 04 傻瓜式安装
  6. 05 [win10中若出现]安装virtualbox快完成时立即回滚,并提示安装出现严重错误
  7. (1)打开服务
  8. (2)找到Device Install ServiceDevice Setup Manager,然后启动
  9. (3)再次尝试安装

1.5.1.3 安装centos7

  1. 01 创建centos7文件夹,并进入其中[目录全路径不要有中文字符]
  2. 02 在此目录下打开cmd,运行vagrant init centos/7
  3. 此时会在当前目录下生成Vagrantfile,同时指定使用的镜像为centos/7,关键是这个镜像在哪里,我已经提前准备好了,名称是virtualbox.box文件
  4. 03 virtualbox.box文件添加到vagrant管理的镜像中
  5. (1)下载网盘中的virtualbox.box文件
  6. (2)保存到磁盘的某个目录,比如D:\virtualbox.box
  7. (3)添加镜像并起名叫centos/7vagrant box add centos/7 D:\virtualbox.box
  8. (4)vagrant box list 查看本地的box[这时候可以看到centos/7]
  9. 04 centos/7镜像有了,根据Vagrantfile文件启动创建虚拟机
  10. 来到centos7文件夹,在此目录打开cmd窗口,执行vagrant up[打开virtual box观察,可以发现centos7创建成功]
  11. 05 以后大家操作虚拟机,还是要在centos文件夹打开cmd窗口操作
  12. vagrant halt 优雅关闭
  13. vagrant up 正常启动
  14. 06 vagrant常用命令
  15. (1)vagrant ssh
  16. 进入刚才创建的centos7
  17. (2)vagrant status
  18. 查看centos7的状态
  19. (3)vagrant halt
  20. 停止/关闭centos7
  21. (4)vagrant destroy
  22. 删除centos7
  23. (5)vagrant status
  24. 查看当前vagrant创建的虚拟机
  25. (6)Vagrantfile中也可以写脚本命令,使得centos7更加丰富
  26. 但是要注意,修改了Vagrantfile,要想使正常运行的centos7生效,必须使用vagrant reload

至此,使用vagrant+virtualbox搭建centos7完成,后面可以修改Vagrantfile对虚拟机进行相应配置

1.5.1.4 若想通过Xshell连接centos7

  1. 01 使用centos7的默认账号连接
  2. centos文件夹下执行vagrant ssh-config
  3. 关注:Hostname Port IdentityFile
  4. IP:127.0.0.1
  5. port:2222
  6. 用户名:vagrant
  7. 密码:vagrant
  8. 文件:Identityfile指向的文件private-key
  9. 02 使用root账户登录
  10. vagrant ssh 进入到虚拟机中
  11. sudo -i
  12. vi /etc/ssh/sshd_config
  13. 修改PasswordAuthentication yes
  14. passwd修改密码,比如abc123
  15. systemctl restart sshd
  16. 使用账号root,密码abc123进行登录

1.5.1.5 Vagrantfile通用写法

  1. # -*- mode: ruby -*-
  2. # vi: set ft=ruby :
  3. # All Vagrant configuration is done below. The "2" in Vagrant.configure
  4. # configures the configuration version (we support older styles for
  5. # backwards compatibility). Please don't change it unless you know what
  6. # you're doing.
  7. Vagrant.configure("2") do |config|
  8. # The most common configuration options are documented and commented below.
  9. # For a complete reference, please see the online documentation at
  10. # https://docs.vagrantup.com.
  11. # Every Vagrant development environment requires a box. You can search for
  12. # boxes at https://vagrantcloud.com/search.
  13. config.vm.box = "centos/7"
  14. # Disable automatic box update checking. If you disable this, then
  15. # boxes will only be checked for updates when the user runs
  16. # `vagrant box outdated`. This is not recommended.
  17. # config.vm.box_check_update = false
  18. # Create a forwarded port mapping which allows access to a specific port
  19. # within the machine from a port on the host machine. In the example below,
  20. # accessing "localhost:8080" will access port 80 on the guest machine.
  21. # NOTE: This will enable public access to the opened port
  22. # config.vm.network "forwarded_port", guest: 80, host: 8080
  23. # Create a forwarded port mapping which allows access to a specific port
  24. # within the machine from a port on the host machine and only allow access
  25. # via 127.0.0.1 to disable public access
  26. # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
  27. # Create a private network, which allows host-only access to the machine
  28. # using a specific IP.
  29. # config.vm.network "private_network", ip: "192.168.33.10"
  30. # Create a public network, which generally matched to bridged network.
  31. # Bridged networks make the machine appear as another physical device on
  32. # your network.
  33. config.vm.network "public_network"
  34. # Share an additional folder to the guest VM. The first argument is
  35. # the path on the host to the actual folder. The second argument is
  36. # the path on the guest to mount the folder. And the optional third
  37. # argument is a set of non-required options.
  38. # config.vm.synced_folder "../data", "/vagrant_data"
  39. # Provider-specific configuration so you can fine-tune various
  40. # backing providers for Vagrant. These expose provider-specific options.
  41. # Example for VirtualBox:
  42. #
  43. # config.vm.provider "virtualbox" do |vb|
  44. # # Display the VirtualBox GUI when booting the machine
  45. # vb.gui = true
  46. #
  47. # # Customize the amount of memory on the VM:
  48. # vb.memory = "1024"
  49. # end
  50. config.vm.provider "virtualbox" do |vb|
  51. vb.memory = "4000"
  52. vb.name= "jack-centos7"
  53. vb.cpus= 2
  54. end
  55. #
  56. # View the documentation for the provider you are using for more
  57. # information on available options.
  58. # Enable provisioning with a shell script. Additional provisioners such as
  59. # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  60. # documentation for more information about their specific syntax and use.
  61. # config.vm.provision "shell", inline: <<-SHELL
  62. # apt-get update
  63. # apt-get install -y apache2
  64. # SHELL
  65. end

1.5.1.6 box的打包分发

  1. 01 退出虚拟机
  2. vagrant halt
  3. 02 打包
  4. vagrant package --output first-docker-centos7.box
  5. 03 得到first-docker-centos7.box
  6. 04 first-docker-centos7.box添加到其他的vagrant环境中
  7. vagrant box add first-docker-centos7 first-docker-centos7.box
  8. 05 得到Vagrantfile
  9. vagrant init first-docker-centos7
  10. 06 根据Vagrantfile启动虚拟机
  11. vagrant up [此时可以得到和之前一模一样的环境,但是网络要重新配置]

1.5.2 安装docker

https://docs.docker.com/install/linux/docker-ce/centos/

  1. 01 进入centos7
  2. vagrant ssh
  3. 02 卸载之前的docker
  4. sudo yum remove docker \
  5. docker-client \
  6. docker-client-latest \
  7. docker-common \
  8. docker-latest \
  9. docker-latest-logrotate \
  10. docker-logrotate \
  11. docker-engine
  12. 03 安装必要的依赖
  13. sudo yum install -y yum-utils \
  14. device-mapper-persistent-data \
  15. lvm2
  16. 04 设置docker仓库 [设置阿里云镜像仓库可以先自行百度,后面课程也会有自己的docker hub讲解]
  17. sudo yum-config-manager \
  18. --add-repo \
  19. https://download.docker.com/linux/centos/docker-ce.repo
  20. [访问这个地址,使用自己的阿里云账号登录,查看菜单栏左下角,发现有一个镜像加速器:https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors]
  21. 05 安装docker
  22. sudo yum install -y docker-ce docker-ce-cli containerd.io
  23. 06 启动docker
  24. sudo systemctl start docker
  25. 07 测试docker安装是否成功
  26. sudo docker run hello-world

1.5.3 docker基本体验

  1. 01 创建tomcat容器
  2. docker pull tomcat
  3. docker run -d --name my-tomcat -p 9090:8080 tomcat
  4. 02 创建mysql容器
  5. docker run -d --name my-mysql -p 3301:3306 -e MYSQL_ROOT_PASSWORD=jack123 --privileged mysql
  6. 03 进入到容器里面
  7. docker exec -it containerid /bin/bash

1.5.4 可能有的疑惑

(1)docker pull在哪拉取的镜像?

  1. 默认是在hub.docker.com

(2)docker pull tomcat拉取的版本是?

  1. 默认是最新的版本,可以在后面指定版本":"

(3)简单先说一下命令咯

  1. docker pull 拉取镜像到本地
  2. docker run 根据某个镜像创建容器
  3. -d 让容器在后台运行,其实就是一个进程
  4. --name 给容器指定一个名字
  5. -p 将容器的端口映射到宿主机的端口
  6. docker exec -it 进入到某个容器中并交互式运行

docker 常用命令

  1. docker 安装mysql
  2. docker pull mysql
  3. docker run -d --name mysql01 -p 3301:3306 -e MYSQL_ROOT_PASSWORD=root --privileged mysql
  4. docker ps 查看
  5. 查看已运行的容器
  6. docker container ls
  7. docker ps
  8. docker container ls -a 获取所有的容器
  9. 进入容器命令
  10. docker exec -it 容器id或者容器名称 /bin/bash
  11. 退出容器
  12. exit
  13. 启动已创建的容器
  14. docker start 容器id/容器名称

navicat 连接mysql8 遇到的问题

问题原因

通过查看网上别人遇到的相关问题,发现是由于navicat版本的问题,出现连接失败的原因:mysql8 之前的版本中加密规则是mysql_native_password,而在mysql8之后,加密规则是caching_sha2_password。并提供了两种解决方案

1.升级navicat,由于navicat是收费的,个人感觉升级会比较麻烦点。

2.把用户密码登录的加密规则还原成mysql_native_password这种加密方式,本人选择第二种解决方案

问题解决

  1. ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;这里的password是你正在使用的密码
  2. ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';#更新一下用户的密码这里的password为你修改的新密码。
  3. FLUSH PRIVILEGES; #刷新权限,使自己的修改生效。
  4. select user,host,plugin from user where user='root';
  5. alter user 'root'@'%' identified by 'password' password expire never;
  6. alter user 'root'@'%' identified with mysql_native_password by 'why';//why是自己新修改的密码。
  7. flush privileges;再次刷新一下权限配置。
  8. --------------------------------------------
  9. alter user 'root'@'localhost' identified by 'root' password expire never;
  10. alter user 'root'@'localhost' identified with mysql_native_password by 'root';
  11. FLUSH PRIVILEGES;
  12. alter user 'root'@'%' identified by 'root' password expire never;
  13. alter user 'root'@'%' identified with mysql_native_password by 'root';
  14. FLUSH PRIVILEGES;