使用EF操做Docker中的Mysql實例

  爲啥我會選擇mysql呢?由於個人服務器配置較低,而SqlServer在docker中的實例,服務器的運行內存應當保持在2G+,我沒有這個條件,它會爆出這樣的錯誤 sqlservr: This program requires a machine with at least 2000 megabytes of memory. 聽我朋友說就算你的機器是2G的,也會報這個錯誤,看了好多網上破解的非常不友好,懼怕出更多的問題,因此天然就選擇了MySql,(SqlServer吃配置仍是很高的)...  mysql

  固然咱們首先也應當在docker中安裝mysql容器,咱們首先能夠經過 docker search mysql 來查詢關鍵字的鏡像。NAME:鏡像倉庫源的名稱、DESCRIPTION:鏡像的描述、OFFICIAL:是否docker官方發佈..若是要看實際的version,則就能夠去https://hub.docker.com/ 中找找了.sql

[root@iZenarrdqnvpc4Z ~]# docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   8995                [OK]                
mariadb                           MariaDB is a community-developed fork of MyS…   3175                [OK]                
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   669                                     [OK]
percona                           Percona Server is a fork of the MySQL relati…   464                 [OK]                
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   66                                      
centurylink/mysql                 Image containing mysql. Optimized to be link…   61                                      [OK]
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   59                                      
deitch/mysql-backup               REPLACED! Please use http://hub.docker.com/r…   41                                      [OK]

下面直接安裝mysql鏡像,經過命令 docker pull mysql:latest  ,裝了個最新版的..哈哈 喜新厭舊嘛...出現了status就ok了...docker

[root@iZenarrdqnvpc4Z ~]# docker pull mysql:latest 
latest: Pulling from library/mysql
804555ee0376: Pull complete 
c53bab458734: Pull complete 
ca9d72777f90: Pull complete 
2d7aad6cb96e: Pull complete 
...............................
Digest: sha256:e1b0fd480a11e5c37425a2591b6fbd32af886bfc6d6f404bd362be5e50a2e632
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest

隨後建立Mysql掛在目錄,用於存放配置、數據、日誌文件,隨後啓動mysql容器並指定剛剛建立的掛在目錄,這兩行是分開執行的。centos

mkdir -p $HOME/mysql/{conf.d,data,logs}
docker run --name mysql -p 3306:3306 -v $HOME/mysql/data:/var/lib/mysql -v $HOME/mysql/conf.d:/etc/mysql/conf.d -v $HOME/mysql/logs:/logs --privileged=true -e MYSQL_ROOT_PASSWORD=123456 -d mysql

進入mysql容器。經過root帳號登陸mysql服務,而後它讓你輸入密碼,這個場景下應該是123456,若是沒問題的話就進去了mysql開頭的tag中。bash

[root@iZenarrdqnvpc4Z ~]# docker exec -it mysql /bin/bash
root@7b96a24b92c2:/# mysql -u root -p #{123456}
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
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> 

若是想要修改root密碼的話,直接把root密碼置空隨後修改密碼。服務器

# 把root密碼置空 
update user set authentication_string = '' where user = 'root' and host = 'localhost';
# 修改root密碼
alter user root@'localhost' identified by '你的密碼';

若是你要添加用戶給別人用,則能夠這麼作。ide

# 添加用戶
create user dev@'%' identified by '123456';
# 給用戶受權
grant Alter, Create, Create Temporary Tables, Create User, Create View, Delete, Drop, Event, Index, Insert, Lock Tables, Process, References, Reload, Select, Show Databases, Show View, Trigger, Update on *.* to dev@'%';
# 刷新權限
flush privileges; 

完事以後你就能夠經過navticat premium 工具進行遠程鏈接了...工具

隨後咱們建立一個.NET Core工程,添加EF關於MySql的相關包,這裏我遇到了一個問題,在我使用 MySql.Data.EntityFrameworkCore 實體移植的時候爆出下面的錯誤,目前還不知道是什麼緣由。在.NET Core 3.0+ 有BUG吧。回頭我去提交個issues ...ui

System.TypeLoadException: Method 'get_Info' in type 'MySql.Data.EntityFrameworkCore.Infraestructure.MySQLOptionsExtension' from assembly 'MySql.Data.EntityFrameworkCore, Version=8.0.18.0, Culture=neutral, 

因此我使用了 pomelo做爲代替它,發現毫無異常。直接code first 開幹。this

public class MysqlDbContext : DbContext
    {
        public DbSet<Student> students { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            //base.OnConfiguring(optionsBuilder);
            string sqlConnection = "server=IP;uid=zaranet;pwd=123456;database=MyDemo";
            optionsBuilder.UseMySql(sqlConnection);
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
        }          
    }

隨後直接 Add-Migration Init ,再update..

PM> Add-Migration Init
To undo this action, use Remove-Migration.
PM> update-database Init

OK,打開navicat 看一下 成功了。

相關文章
相關標籤/搜索