【ASP.NET Core分布式項目實戰】(四)使用mysql/mysql-server安裝mysql


Docker安裝Mysql

  1. 拉取鏡像

    docker pull mysql/mysql-server
  2. 運行mysql

    docker run -d -p 3306:3306 --name mysql01 mysql/mysql-server
  3. 查看密碼

    docker logs mysql01
  4. 進入容器

    docker exec -it mysql01 bash
  5. 進入mysql的命令行

    mysql -uroot -p
  6. 修改root密碼
    use mysql;
    ALTER USER 'root'@'localhost' IDENTIFIED BY 'pwd123456';

創建新用戶

CREATE USER 'test'@'localhost' IDENTIFIED BY 'pwd123456';
GRANT ALL PRIVILEGES ON *.* TO 'test'@'localhost' WITH GRANT OPTION;
CREATE USER 'test'@'%' IDENTIFIED BY 'pwd123456';
GRANT ALL PRIVILEGES ON *.* TO 'test'@'%' WITH GRANT OPTION;
# navicat連接mysql報錯1251解決方案
# 更改加密方式
ALTER USER 'root'@'localhost' IDENTIFIED BY 'pwd123456' PASSWORD EXPIRE NEVER;
ALTER USER 'root'@'%' IDENTIFIED BY 'pwd123456' PASSWORD EXPIRE NEVER;
# 重新修改密碼
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'pwd123456';
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'pwd123456';
# 刷新
FLUSH PRIVILEGES;

修改MYSQL 默認字符集

  1. 查看字符集命令

    docker exec -it mysql01 bash
    mysql -uroot -ppwd123456
    use mysql;
    show variables like '%char%';
    +--------------------------+--------------------------------+
    | Variable_name            | Value                          |
    +--------------------------+--------------------------------+
    | character_set_client     | latin1                         |
    | character_set_connection | latin1                         |
    | character_set_database   | utf8mb4                        |
    | character_set_filesystem | binary                         |
    | character_set_results    | latin1                         |
    | character_set_server     | utf8mb4                        |
    | character_set_system     | utf8                           |
    | character_sets_dir       | /usr/share/mysql-8.0/charsets/ |
    +--------------------------+--------------------------------+

     

  2. 修改my.cnf

    [mysqld]
    character-set-server=utf8mb4
    [client]
    default-character-set=utf8mb4 
    [mysql]
    default-character-set=utf8mb4

docker容器參數啟動Mysql

  1. 創建容器
    docker run -d -p 3306:3306 -e MYSQL_USER="jeese" -e MYSQL_PASSWORD="pwd123456" -e MYSQL_ROOT_PASSWORD="pwd123456" -e MYSQL_ROOT_HOST=% --restart=always --name mysql01 mysql/mysql-server --character-set-server=utf8 --collation-server=utf8_general_ci
  2. 修改navicat連接

    docker exec -it mysql01 bash
    mysql -uroot -ppwd123456 use mysql; # navicat連接mysql報錯1251解決方案 ALTER USER
    'jeese'@'%' IDENTIFIED WITH mysql_native_password BY 'pwd123456'; ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'pwd123456'; # 刷新 FLUSH PRIVILEGES;

MySql掛載資料卷

  1. mysql掛載資料卷
    #注意:需要先創建/docker/mysql/config/my.cnf文件和/docker/mysql/data文件夾
    cd /
    mkdir docker
    cd docker
    mkdir mysql
    cd mysql
    mkdir config
    mkdir data
    vim config/my.cnf
    
    [mysqld]
    user=mysql
    character-set-server=utf8
    [client]
    default-character-set=utf8
    [mysql]
    default-character-set=utf8
    
    # mysql掛載資料卷
    docker run -d -p 3306:3306 -v /docker/mysql/config/my.cnf:/etc/my.cnf -v /docker/mysql/data:/var/lib/mysql -e MYSQL_USER="jeese" -e MYSQL_PASSWORD="pwd123456" -e MYSQL_ROOT_PASSWORD="pwd123456" -e MYSQL_ROOT_HOST=% --restart=always --name mysql01 mysql/mysql-server --character-set-server=utf8 --collation-server=utf8_general_ci

     

  2. 修改navicat連接

    docker exec -it mysql01 bash
    mysql -uroot -ppwd123456 use mysql; # navicat連接mysql報錯1251解決方案 ALTER USER
    'jeese'@'%' IDENTIFIED WITH mysql_native_password BY 'pwd123456'; ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'pwd123456'; # 刷新 FLUSH PRIVILEGES;

mongo掛載資料卷

cd /docker/
mkdir mongo
cd mongo
mkdir config
mkdir db

docker run -d -p 27017:27017 --name mongo01 -v /docker/mongo/config:/data/configdb -v /docker/mongo/db:/data/db --restart=always mongo

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM