安装MongoDB 安装 symfony 组件过程中踩过的坑后期补上
感觉这方面的资源太难找,先记录一下,方便自己使用,也方便一起交流学习。
安装MongoDB:
window系统下,没有安装到系统盘的,程序安装后不能启动服务,去找一个配置文件删除最后一行
PHP 安装MongoDB dll扩展组件:
一定要找对dll,系统是32位(x86)还是64位(x64)不能用错了,线程 安全和非线程安全不能用错了,php版更不能找错了,因为打开网站速度太慢,我把php7.2-7.4的扩展都下了,需要的M
linux下,我用的是国产deepin系统,环境使用oneinstack傻瓜安装模式(贼傻)
mongodb安装倒是很顺利,但是跟windows下的毛病是一样的,php 扩展一样是问题,还是从官网找
直接贴地址不如告诉流程,windows 和 linux都来这里找,
http://pecl.php.net/package-search.php 进入后搜索mongodb,别搞倒mongo那里去
window进入图标后dll下载最新版本的dll,下载后直接使用dll很方便
linux直接下载那个压缩包,解压后一脸懵逼,mongodb.so去哪里找?恩,去百度找?no!!!压缩包有个readme.md文件,一般规范的文件都能找出有效的信息,安装指引人家让参考
https://www.php.net/manual/en/mongodb.installation.manual.php
$ git clone https://github.com/mongodb/mongo-php-driver.git
$ cd mongo-php-driver
$ git submodule update --init
$ phpize
$ ./configure
$ make all
$ sudo make install
这里因为我们直接下载了文件就不用再克隆了
直接走后面的步骤,结果OK的一切顺利,当然还要修改php.ini文件,重启php-fpm才能生效
关于MongoDB权限和认证
默认安装好的mongodb应该是没有密码的(至少我是这样的),随便访问,如果通过oneinstack安装的应该有一个root账号和密码的设置
但是使用程序或者登录的时候你会发现会出现错误,程序上面的报错如下
not authorized on exam to execute command { find: "Category", filter: {}, $db: "exam", lsid: { id: UUID("4ec77d0a-cb40-4dbd-8fcb-621748b60a0b") } }
修改这部分功能首先要开启不验证的模式
# mongod.conf 配置文件中找到
security:
authorization: enabled
把enabled 改成disabled
重启mongodb,然后进入命令行设置相关内容
进入 :/usr/local/mongodb/bin/mongo --host 127.0.0.1:27017
切换数据库:use admin (这个数据库是唯一的)
授权:db.auth('用户名','用户密码')
修改权限:db.grantRolesToUser("root", [{role:"readWriteAnyDatabase", db:"admin"}])
OK结束
做其他修改这里可能用到的命令有
创建用户--(这里我尝试为每个数据设置单独的账号没有成功,后面有时间再研究一下),底下的格式是创建用户的格式
db.createUser( {user: "exam",pwd: "123456",roles: [ { role: "dbAdmin", db: "exam" },{ role: "userAdmin", db: "exam" } ]})
设置root为超级权限用户
db.grantRolesToUser("root", [{role:"readWriteAnyDatabase", db:"admin"}])
相关操作手册,用操作用到的函数方法查询下面的链接,(手册还是要读啊)
https://docs.mongodb.com/v3.2/reference/method/js-user-management/
记得把配置文件的验证打开,重启
yaml配置信息
server:"%mongodb_server%"
options:
username:"root"
password:"root123"
authSource: admin
如果使用URI配置应该是
mongodb://root:root123@localhost:27017/?authSource=admin
至于这里面的authSource这个东西需要详细了解下不然弄不懂这里的设置
单独数据库单独配置账号密码后期再爬坑
symfony配置MongoDB先不写:后期补
关于ORM Entity 和 ODM Document同时使用,还在尝试,希望不冲突,毕竟console那一套挺好用
关于字段的annotation注释
Id主键的可选项如下
AUTO - Uses the native generated ObjectId.
ALNUM - Generates an alpha-numeric string (based on an incrementing value).
CUSTOM - Defers generation to a AbstractIdGenerator implementation specified in the class option.
INCREMENT - Uses another collection to auto increment an integer identifier.
UUID - Generates a UUID identifier.
NONE - Do not generate any identifier. ID must be manually set.
文档中对应的代码
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MG;
/**
* @Groups({"app"})
* @MG\Id(strategy="INCREMENT")
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
参考地址
字段常用属性Field(type='')
bin: string to MongoDB\BSON\Binary instance with a "generic" type (default)
bin_bytearray: string to MongoDB\BSON\Binary instance with a "byte array" type
bin_custom: string to MongoDB\BSON\Binary instance with a "custom" type
bin_func: string to MongoDB\BSON\Binary instance with a "function" type
bin_md5: string to MongoDB\BSON\Binary instance with a "md5" type
bin_uuid: string to MongoDB\BSON\Binary instance with a "uuid" type
collection: numerically indexed array to MongoDB array
date: DateTime to MongoDB\BSON\UTCDateTime
date_immutable: DateTimeImmutable to MongoDB\BSON\UTCDateTime
hash: associative array to MongoDB object
id: string to ObjectId by default, but other formats are possible
timestamp: string to MongoDB\BSON\Timestamp
raw: any type
关于一对一,一对多,多对一,多对多
分两种 Reference 和 Embedded 两种情况讨论
Reference 属于只引用的形式
Embedded 属于直接关联,看文档好像是说 Document(子文档)是不能独立存在的,