使用EF操作Docker中的Mysql实例
为啥我会选择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,则就可以去 中找找了.
[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 看一下 成功了。