docker安装及docker部署MySQL8.0版本---------一眼就会

1、docker安装

安装docker(需联网)

[root@localhost ~]# curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

启动docker,并设置开机自启

[root@localhost ~]# systemctl start docker
[root@localhost ~]# systemctl enable docker

2、docker安装MySQL8.0.18

拉取MySQL镜像

[root@localhost ~]# docker pull mysql:8.0.18

查看是否拉取成功

[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
mysql        8.0.18    ed1ffcb5eff3   2 years ago   456MB

创建数据库目录

[root@localhost ~]# mkdir -p /home/docker/docker-data/mysql-data
[root@localhost ~]# cd /home/docker/docker-data/mysql-data
[root@localhost ~]# mkdir log
[root@localhost ~]# mkdir data
[root@localhost ~]# chmod 777 log
[root@localhost ~]# chmod 777 data
[root@localhost ~]# mkdir my.conf.d
[root@localhost ~]# cd my.conf.d
[root@localhost ~]# touch my.cnf

添加MySQL配置文件(my.cnf)

[root@localhost ~]# vim my.cnf
[client]
# 默认字符集
#default-character-set=utf8
[mysql]
# 默认字符集
#default-character-set=utf8
[mysqld]
# 
server-id=01
port=3306
## 开启二进制日志功能
log-bin=mysql-slave-bin
## relay_log配置中继日志
relay_log=edu-mysql-relay-bin
# 忽略大小写
lower_case_table_names=1
pid-file= /var/run/mysqld/mysqld.pid
socket= /var/run/mysqld/mysqld.sock
# 数据库数据存放目录
datadir= /var/lib/mysql
secure-file-priv= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
skip-symbolic-links=0
# 最大链接数
max_connections=200
# 最大失败次数
max_connect_errors=10
# 默认时区
default-time_zone='+8:00'
character-set-client-handshake=FALSE
character_set_server=utf8mb4
# default-character-set=utf8
collation-server=utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci'
# 默认使用‘mysql_native_password’插件认证
default_authentication_plugin= mysql_native_password
# Custom config should go here
!includedir /etc/mysql/conf.d/

创建容器并启动

docker run \
    -p 3308:3306 \
    -e MYSQL_ROOT_PASSWORD=123456 \
    -v /home/docker/docker-data/mysql-data/data:/var/lib/mysql:rw \
    -v /home/docker/docker-data/mysql-data/log:/var/log/mysql:rw \
    -v /home/docker/docker-data/mysql-data/my.conf.d/my.cnf:/etc/mysql/my.cnf:rw \
    --name mysql \
    --privileged=true \
    --restart=always \
    -d mysql:8.0.18

# -p 映射宿docke端口号到宿主机端口号  左:宿主机端口号 右:docker端口号
# -e MYSQL_ROOT_PASSWORD  设置root用户密码
# -v 挂载点  左边为挂载宿主机目录 右边为容器目录 :rw 为可读写  

查看docker容器

[root@localhost]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED      STATUS          PORTS                                                  NAMES
8716af3eed19   mysql:8.0.18   "docker-entrypoint.s…"   3 days ago   Up 15 minutes   33060/tcp, 0.0.0.0:3308->3306/tcp, :::3308->3306/tcp   mysql

进入容器连接MySQL

[root@localhost ~]# docker exec -it 8716af3eed19 bash
root@8716af3eed19:/# 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.18 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> 

至此docker部署MySQL就完成了

3、使用navicat连接MySQL

开启防火墙,并开通服务器数据库3308端口

[root@localhost ~]# systemctl start firewalld
[root@localhost ~]# systemctl enable firewalld
[root@localhost ~]# firewall-cmd --add-port=3308/tcp
success
[root@localhost ~]# 

测试连接

docker安装及docker部署MySQL8.0版本---------一眼就会_第1张图片

其他账号登录,需要创建账号,账号具体权限以实际为准(这里给了最大权限)

mysql> CREATE USER 'docker_mysql'@'%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.41 sec)

mysql> GRANT ALL ON *.* TO 'docker_mysql'@'%';
Query OK, 0 rows affected (0.09 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.12 sec)

创建完成,可以使用登录!

你可能感兴趣的:(docker,docker,mysql)