共计 3934 个字符,预计需要花费 10 分钟才能阅读完成。
为啥我会选择 MySQL 呢?因为我的服务器配置较低,而 SqlServer 在 docker 中的实例,服务器的运行内存应当保持在 2G+, 我没有这个条件, 它会爆出这样的错误 sqlservr: This program requires a machine with at least 2000 megabytes of memory. 听我朋友说就算你的机器是 2G 的,也会报这个错误,看了好多网上破解的很是不友好,害怕出更多的问题,所以自然就选择了 MySql,(SqlServer 吃配置还是很高的)…
当然我们首先也应当在 docker 中安装 mysql 容器,我们首先可以通过 docker search mysql 来查询关键字的镜像。NAME: 镜像仓库源的名称、DESCRIPTION: 镜像的描述、OFFICIAL: 是否 docker 官方发布.. 如果要看实际的 version,则就可以去 https://hub.docker.com/ 中找找了.
[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 了 …
[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 容器并指定刚刚创建的挂在目录,这两行是分开执行的。
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 中。
[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 ‘ 你的密码 ’;
如果你要添加用户给别人用,则可以这么做。
# 添加用户
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 …
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 开干。
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 看一下 成功了。
: