Set up a MySQL environment on the cloud server and set up remote access users with different permissions

Set up a MySQL environment on the cloud server and set up remote access users with different permissions

1. Install docker under CentOS7.6

  1. update source

    yum -y update
    
  2. Install docker

    yum -y install docker
    
  3. Start docker and set it to start automatically at boot

    systemctl start docker
    systemctl enable docker
    
  4. Check docker version

    docker info
    

2. Use docker to install mysql8.0

  1. Download mysql8.0 image

    docker pull mysql:8.0
    
  2. View image

    docker images
    
  3. Create mount directory

    mkdir -p /data/mysql8/conf
    mkdir -p /data/mysql8/data
    mkdir -p /data/mysql8/logs
    
  4. Create the my.cnf file and place it in the /data/mysql/conf directory. Pay attention to the port number, character set, and time zone in the configuration file. Since I am not very good at setting my.cnf here, I will ignore it here.

  5. Create and start the container, please refer to it appropriately

    docker run -d --name mysql8 \
    -e MYSQL_ROOT_PASSWORD=123456 \
    -v /data/mysql8/data:/var/lib/mysql \
    -v /data/mysql8/logs:/var/log/mysql \
    -p 53306:3306 \
    --restart=unless-stopped \
    mysql:8.0
    

    Parameter explanation:

    • -d: Let the container run in the background
    • --name: What to name the container
    • -p: Mapping the docke port number to the host port number Left: Host port number Right: Docker port number
    • -e MYSQL_ROOT_PASSWORD=:Set root user password
    • -v: Mount point. The left side is the mounting host directory and the right side is the container directory.
    • --restart: Specify the policy for automatic container restart.
      • unless-stopped: After the container is started, it will always restart automatically as long as it is not stopped manually.
      • always: The container always restarts automatically when it exits. Even if the container has been manually stopped under normal circumstances, it will be automatically restarted.
      • on-failure[:max-retries]: If the exit status of the container is not 0, it will automatically restart up to max-retries times (this parameter is optional and the default value is 5). If max-retries is set to 0, the container is automatically restarted an unlimited number of times.
      • no: Never restart the container
  6. Enter the container:docker exec -it 容器id /bin/bash

    eg:docker exec -it 00c1820f12ef /bin/bash

  7. Enter mysql: mysql -uroot -p, then enter the password

3. Set up remote access users with different access rights

Here I re-ran a new mysql8 container

image-20230618215203562

  1. Enter the container

    docker exec -it 788b531e4441 /bin/bash
    
  2. Enter mysql

    mysql -uroot -p
    

    Then enter the password

  3. Some simple operations to view the login usernames and corresponding matching hosts

    # 列出所有数据库的列表
    SHOW databases;
    #显示指定数据库的所有表,前提已经使用use选择了操作的数据库
    USE mysql;
    SHOW TABLES;
    #查询用户表中可登录用户名和匹配的主机
    SELECT User,Host from user;
    

    image-20230618221358006

    Found a userroot who can log in to the host%, from any source. Since the password I set is very simple, we need to delete this user. Create other users with specific permissions to increase database security and allow root users to only log in locally on the server. 123456

    Delete this user:

    DROP USER 'root'@'%';
    
  4. Create a database for testingdatespase_test

    CREATE DATABASE datespase_test;
    
  5. Create userettian, the host source that can log in is%

    CREATE USER 'ettian'@'%' IDENTIFIED BY 'test_password';
    

    test_passwordPassword for this user

  6. Grantdatespase_testall permissions under the database

    grant all privileges on datespase_test.* to 'ettian'@'%';
    
  7. Refresh permissions

    flush privileges;
    

    image-20230618223258486

  8. Use Navicat to connect, remember to open the corresponding port number in the server's firewall.

    image-20230618223858276

    Since the local port number mapped by my container is 53307, I wrote it as 3306 in this picture, so the connection cannot be made. Edit the connection and change it to 53307.

    image-20230618224136657

Guess you like

Origin blog.csdn.net/zhihong2002/article/details/131277278