Docker--install mysql8--method/step

Original website: Docker--install mysql8--method/step_IT sharp knife unsheathed blog-CSDN blog

Introduction

        This article describes how to install mysql8 using docker.

Check mysql version

https://hub.docker.com/_/mysql/tags

I choose 8.0, and click the arrow on the right to directly copy the command.

pull image

command to pull image

docker pull mysql:8.0

Check if the pull is successful

docker images

The following is the success of the pull

Create folders (config, data, etc.)

It is not necessary here, because when you use docker run to run the container below, it will automatically create folders that do not exist according to the -v parameter.

mkdir -p /work/docker/mysql/conf
mkdir -p /work/docker/mysql/data
mkdir -p /work/docker/mysql/log

Create configuration file

Create file: /work/docker/mysql/conf/my.cfg

The content is as follows:

[client]
default-character-set=utf8mb4

[mysql]
default-character-set=utf8mb4

[mysqld]
#服务端口号 默认3306
port=3306

datadir = /work/docker/mysql/data

init_connect='SET NAMES utf8mb4'
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci

# 最大连接数
max_connections=200

# 连接失败的最大次数。防止有人从该主机试图攻击数据库系统
max_connect_errors=20

# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB

Start the container

The command to start the container

docker run -p 3306:3306  \
--name mysql8 \
--privileged=true \
-v /work/docker/mysql/log:/var/log/mysql \
-v /work/docker/mysql/data:/var/lib/mysql \
-v /work/docker/mysql/conf:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql:8.0
  • –privileged=true
    • Set the root user authority of MySQL, otherwise the root user cannot be used to log in externally.
  • -e MYSQL_ROOT_PASSWORD=123456
    • Set the password of the MySQL database root user

For other parameters, refer to this article: Docker--Installing Redis--Methods/Steps

View the started container

docker ps

set password etc.

        In fact, the original 123456 is still valid, but it can only be used on the localhost machine (it can be used when the mysql command on the MySQL server is directly connected) and Navicat cannot be used.

1. Get inside the container

docker exec -it mysql8 /bin/bash

2. connect to mysql

mysql -u root -p

Enter the password set earlier: 123456, then press Enter.

3. Modify the access host and password

Make it accessible to all hosts

ALTER USER 'root'@'%' IDENTIFIED BY '新密码';

Mysql8.0 uses caching-sha2-password encryption by default, which may not be supported by old clients, and can be changed to mysql_native_password; 

CREATE USER 'root'@'%' IDENTIFIED WITH MYSQL_NATIVE_PASSWORD BY '222333';

4. Refresh permissions

FLUSH PRIVILEGES;

Test Navicat connection

The password is 222333. connection succeeded.

Guess you like

Origin blog.csdn.net/feiying0canglang/article/details/127776335