一、Mysql8安装包下载

1、迅雷复制链接下载
https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz

2、百度网盘获取
https://pan.baidu.com/s/1vxVEL7q7CQvPPLlK-ZUWEQ

提取码:whrs

3、mysql官网下载

https://www.mysql.com/downloads/

注意:迅雷下载链接和网盘获取的版本都是8.0.22吗,自己下载的话,记得相关命令中改掉版本号

二、安装步骤

1.上传安装包

1)、将安装包上传至服务器的 /usr/local 文件夹下

[root@localhost ~]# cd /usr/local
[root@localhost local]# ls
bin  etc  games  include  lib  lib64  libexec  mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz   sbin  share  src

2)、解压

[root@localhost local]# tar -xvf mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz

3)、更改文件夹名称

[root@localhost local]# mv mysql-8.0.20-linux-glibc2.12-x86_64 mysql

4)、修改配置文件

[root@localhost local]# vim /etc/my.cnf

全部copy进去

[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
character-set-server=utf8
sql_mode =STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION

:wq 回车保存退出

5)、新建用户和用户组并进入mysql 文件下赋予权限

[root@localhost mysql]# cd /usr/local/mysql/
[root@localhost mysql]# groupadd mysql
[root@localhost mysql]# useradd -r -g mysql mysql
[root@localhost mysql]# chown -R mysql:mysql ./

6)、初始化数据库

[root@localhost mysql]# mkdir ./data
[root@localhost mysql]# ./bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --initialize ;

在这里插入图片描述

初始化之后,最后一行有初始化密码。需要记住

7)、把msyql 添加到系统服务

[root@localhost mysql]# cp -a ./support-files/mysql.server /etc/init.d/mysql
[root@localhost mysql]# chmod +x /etc/init.d/mysql #权限
[root@localhost mysql]# chkconfig --add mysql
[root@localhost mysql]# chkconfig --list mysql #检查是否生效

8)、建立软连接

[root@localhost bin]# ln -s /usr/local/mysql/bin/mysql /usr/bin

9)、启动MYSQL服务

[root@localhost mysql]# service mysql start
Starting MySQL.. SUCCESS! 

10)、登陆Mysql
mysql -u root -p

mysql -u root -p

输入刚刚的密码(一定要盲输入密码,直接拼接在-p后面会无效)

13)、修改密码

ALTER USER "root"@"localhost" IDENTIFIED  BY "你的新密码";
FLUSH PRIVILEGES;  #配置生效

14)、打开连接 数据库连接工具

use mysql;  
update user set host='%' where user ='root';
FLUSH PRIVILEGES;    #配置生效

三、docker安装

安装所需的包。yum-utils提供了yum-config-manager 效用,并device-mapper-persistent-data和lvm2由需要 devicemapper存储驱动程序

1、[root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2

设置稳定存储库

2、[root@localhost ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

安装Docker CE

3、[root@localhost ~]# yum install -y docker-ce docker-ce-cli containerd.io

验证docker安装是否成功

[root@localhost ~]# docker --version
Docker version 19.03.13, build 4484c46d9d

3.1、配置加速器

mkdir /etc/docker
cd /etc/docker
vim daemon.json
#以下内容粘贴进daemon.json中
{
 "registry-mirrors" : [
   "https://mirror.ccs.tencentyun.com",
   "http://registry.docker-cn.com",
   "http://docker.mirrors.ustc.edu.cn",
   "http://hub-mirror.c.163.com"
 ],
 "insecure-registries" : [
   "registry.docker-cn.com",
   "docker.mirrors.ustc.edu.cn"
 ],
 "debug" : true,
 "experimental" : true
}
#重启
systemctl restart docker.service
docker pull mysql
#启动
docker run --name mysql -p 3306:3306 -v /mysql/datadir:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456  -d mysql

参数说明:

--name: 以什么名字启动容器
-p 3306:3306 :将容器端口映射到服务器端口

-v /mysql/datadir:/var/lib/mysql :将mysql的配置路径映射到本地datadir上

-e MYSQL_ROOT_PASSWORD=123456 :设置服务器密码为123456

-d mysql:需要启动的容器的名称

[root@localhost docker]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                               NAMES
680af9d51d7b   mysql     "docker-entrypoint.s…"   15 minutes ago   Up 15 minutes   0.0.0.0:3306->3306/tcp, 33060/tcp   mysql

#进入容器内部并登录mysql
[root@localhost docker]# docker exec -it 680af9d51d7b sh

# mysql -uroot -p123456
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 13
Server version: 8.0.23 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> 
Logo

更多推荐