Install mysql redis nginx nacos using Docker image

Docker image installation practice

Install MySql database

Installation steps
Step 1: search the mysql image on hub.docker.com

Step 2: pull the specified version of mysql. You can also specify the pull version, for example:

 docker pull mysql:8.0.23

Step 3: check the mysql image

docker images

Step 4: start and run mysql image (docker run is used to start a container)

sudo docker run -p 3306:3306 --name mysql \
-v /usr/local/docker/mysql/mysql-files:/var/lib/mysql-files \
-v /usr/local/docker/mysql/conf:/etc/mysql \
-v /usr/local/docker/mysql/logs:/var/log/mysql \
-v /usr/local/docker/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:8.0.23

If the installation fails, you can view the previous container through docker ps -a. if it already exists, you can delete it through docker rm image id and reinstall it.

Log in to mysql service
Step 1: enter the container (exit for exiting the container)

sudo docker exec -it mysql bash

Step 2: log in (the default password is root), and be sure to enter the mysql container first.

mysql -uroot -proot

Stop and start mysql service
Stop mysql service (optional)

docker stop mysql

service mysql start

docker start mysql

If you want to view the log when MySQL is started, you can execute the command docker container logs mysql.

Set mysql startup self startup (optional)

docker update mysql --restart=always

The remote tool connects to the mysql container in docker and imports the database

  • If the connection fails, authorize the mysql container
    Possible problems
    If you use the client connection tool to connect at this time, you will encounter the error of Client does not support authentication protocol.

The solution is to set up remote login and let's continue.

2. Set up remote login

2.1 enter the MySQL container and log in to MySQL

 docker exec -it mysql /bin/bash

2.2 login mysql**

  mysql -u root -p

2.3 enter password
At this time, you will be prompted to enter the password.

2.4 remote connection authorization

GRANT ALL ON *.* TO 'root'@'%';

Refresh permissions

flush privileges

Note that remote access is not available at this time
Because Navicat only supports the old version of encryption, you need to change the encryption rules of mysql

2.5 changing encryption rules

ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;

Update root user password

ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';

Refresh permissions

flush privileges;

Then you can connect to the database using Navicat.

Install Redis database

Installation steps
Step 1: download the image file

docker pull redis #The latest version is pulled by default

Step 2: prepare the configuration file

Create redis profile directory

mkdir -p /usr/local/docker/redis01/conf

Create a redis.conf configuration file under the configuration file record (this file must be created. No, a directory is generated by default when we mount the directory)

touch /usr/local/docker/redis01/conf/redis.conf

Step 3: create a redis instance and start it

sudo docker run -p 6379:6379 --name redis01 \
-v /usr/local/docker/redis01/data:/data \
-v /usr/local/docker/redis01/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf 

Step 4: view the running process

docker ps

Access redis server
Step 1: connect the console directly to redis test

docker exec -it redis01 bash

Step 2: check the redis version

redis-server  -v

perhaps

redis-cli -v

Step 3: log in to redis (no password is required by default)

redis-cli

Or you can directly combine the above two steps into one step. The instructions are as follows:

docker exec -it redis01 redis-cli

Stop and start redis service
Stop redis service?

docker stop redis01

Start redis service?

docker start redis01

Restart redis service?

docker restart redis01

Install Nginx agent

Installation steps
Step 1: pull the nginx image (find it here at hub.docker.com)

docker pull nginx

Step 2: view the images Image

docker images

Step 3: create a data volume (this object will directly create a directory on the host)

docker volume create nginx-vol

Note: to view the host directory corresponding to the data volume, you can use the following instructions:

docker inspect nginx-vol

Step 4: start nginx service

docker run --name nginx  -p 80:80 -v nginx-vol:/etc/nginx -d nginx

Where: / etc/nginx is the default decompression directory of nginx image files when the nginx container is started

Note: if you want to modify the nginx configuration in the future, you can modify it directly in the directory corresponding to the nginx Vol data volume

Accessing nginx services
Perform access detection, as shown in the figure:

Stop and nginx service

Stop nginx service

docker stop nginx

Start nginx service

docker start nginx

Restart nginx service

docker restart nginx

Installing Nacos components

Installation steps
Step 1: pull nacos (hub.docker.com)

docker pull nacos/nacos-server:1.4.1

Step 2: execute the sql script file of nacos in mysql

1) Copy the file nacos-mysql.sql (which can be downloaded from the code server) to the mount directory corresponding to the host of the MySQL container (you can view your MySQL mount directory through docker inspect mysql)

2) Start and log in to mysql in linux environment

Enter mysql container (if mysql has been started)

docker exec -it mysql bash

Login to mysql

mysql -uroot -p

3) Run the sql file in the container directory through the source command

  • The built-in SQL file does not have a command statement to create a database
Link: https://pan.baidu.com/s/13YO46XJu4YCYaWrXC6CGGQ 
Extraction code: d4nn
source  /etc/mysql/nacos-mysql.sql  #Here, / etc/mysql is a directory in the container (select the directory you want to mount)
#Import the nacos-mysql.sql file into the mount directory in the MySQL container. When entering the MySQL import data file, the imported container directory corresponds to the mount directory

docker run  \
-e TZ="Asia/Shanghai" \
-e MODE=standalone \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_DATABASE_NUM=1 \
-e MYSQL_SERVICE_HOST=192.168.126.129 \ #Virtual machine ip
-e MYSQL_SERVICE_PORT=3306 \
-e MYSQL_SERVICE_USER=root \  #Own mysql user name
-e MYSQL_SERVICE_PASSWORD=root \ #Own mysql password
-e MYSQL_SERVICE_DB_NAME=nacos_config \ #You can import nacos related SQL files
-p 8848:8848 \
--name nacos \
--restart=always \
-d nacos/nacos-server:1.4.1
Parameter description

Single node mode
MODE=standalone
Database address
MYSQL_SERVICE_HOST
Database user name
MYSQL_SERVICE_USER
Database password
MYSQL_SERVICE_PASSWORD
Name of database to be connected
MYSQL_SERVICE_DB_NAME
Port mapping
-p 8848:8848
Restart the container at any time, and the container can be started automatically (docker needs to be set to start automatically)

--restart=always

Step 4: check the nacos service

docker ps

If the startup fails, check the startup log, for example:

docker container logs nacos

The startup log of nacos is in the / home/nacos/logs/start.out file.

Accessing nacos services
Start Nacos, and then enter http://ip:port/nacos Access test

Note: when nacos logs in, the default user name and password are both nacos

Analysis of communication between Nacos and MySQL

Stop and start the nacos service

Stop the nacos service

docker stop nacos

Start the nacos service

docker start nacos

Restart the nacos service

docker restart nacos

The network pull-up is slow and error prone. Offline installation can be considered

  • Related image package
Link: https://pan.baidu.com/s/1KfJ8hh3RJBFTI7NDA-btfQ 
Extraction code: p5za
  • Related commands
  • Import mirror
docker load < hello-world.tar.gz  
  • Run mirror file
docker run hello-world #If the image is imported, the version number must be added, or the latest version will be downloaded from the remote warehouse

Tags: MySQL Docker Redis

Posted on Thu, 07 Oct 2021 22:40:54 -0400 by rempires