Symfony5.0 中使用MongoDB學(xué)習(xí)筆記

安裝MongoDB?安裝 symfony?組件過程中踩過的坑后期補(bǔ)上

感覺這方面的資源太難找,先記錄一下,方便自己使用,也方便一起交流學(xué)習(xí)。


安裝MongoDB:

window系統(tǒng)下,沒有安裝到系統(tǒng)盤的,程序安裝后不能啟動(dòng)服務(wù),去找一個(gè)配置文件刪除最后一行

PHP?安裝MongoDB dll擴(kuò)展組件:

一定要找對(duì)dll,系統(tǒng)是32位(x86)還是64位(x64)不能用錯(cuò)了,線程?安全和非線程安全不能用錯(cuò)了,php版更不能找錯(cuò)了,因?yàn)榇蜷_網(wǎng)站速度太慢,我把php7.2-7.4的擴(kuò)展都下了,需要的M


linux下,我用的是國(guó)產(chǎn)deepin系統(tǒng),環(huán)境使用oneinstack傻瓜安裝模式(賊傻)

mongodb安裝倒是很順利,但是跟windows下的毛病是一樣的,php 擴(kuò)展一樣是問題,還是從官網(wǎng)找

直接貼地址不如告訴流程,windows 和 linux都來這里找,

http://pecl.php.net/package-search.php? 進(jìn)入后搜索mongodb,別搞倒mongo那里去

window進(jìn)入圖標(biāo)后dll下載最新版本的dll,下載后直接使用dll很方便

linux直接下載那個(gè)壓縮包,解壓后一臉懵逼,mongodb.so去哪里找?恩,去百度找?no!!!壓縮包有個(gè)readme.md文件,一般規(guī)范的文件都能找出有效的信息,安裝指引人家讓參考

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

這里因?yàn)槲覀冎苯酉螺d了文件就不用再克隆了

直接走后面的步驟,結(jié)果OK的一切順利,當(dāng)然還要修改php.ini文件,重啟php-fpm才能生效

關(guān)于MongoDB權(quán)限和認(rèn)證

默認(rèn)安裝好的mongodb應(yīng)該是沒有密碼的(至少我是這樣的),隨便訪問,如果通過oneinstack安裝的應(yīng)該有一個(gè)root賬號(hào)和密碼的設(shè)置

但是使用程序或者登錄的時(shí)候你會(huì)發(fā)現(xiàn)會(huì)出現(xiàn)錯(cuò)誤,程序上面的報(bào)錯(cuò)如下

not authorized on exam to execute command { find: "Category", filter: {}, $db: "exam", lsid: { id: UUID("4ec77d0a-cb40-4dbd-8fcb-621748b60a0b") } }

修改這部分功能首先要開啟不驗(yàn)證的模式

# mongod.conf 配置文件中找到

security:

? authorization: enabled

把enabled 改成disabled

重啟mongodb,然后進(jìn)入命令行設(shè)置相關(guān)內(nèi)容

進(jìn)入 :/usr/local/mongodb/bin/mongo --host 127.0.0.1:27017

切換數(shù)據(jù)庫(kù):use admin? (這個(gè)數(shù)據(jù)庫(kù)是唯一的)

授權(quán):db.auth('用戶名','用戶密碼')

修改權(quán)限:db.grantRolesToUser("root", [{role:"readWriteAnyDatabase", db:"admin"}])

OK結(jié)束


做其他修改這里可能用到的命令有

創(chuàng)建用戶--(這里我嘗試為每個(gè)數(shù)據(jù)設(shè)置單獨(dú)的賬號(hào)沒有成功,后面有時(shí)間再研究一下),底下的格式是創(chuàng)建用戶的格式

db.createUser( {user: "exam",pwd: "123456",roles: [ { role: "dbAdmin", db: "exam" },{ role: "userAdmin", db: "exam" } ]})

設(shè)置root為超級(jí)權(quán)限用戶

db.grantRolesToUser("root", [{role:"readWriteAnyDatabase", db:"admin"}])

相關(guān)操作手冊(cè),用操作用到的函數(shù)方法查詢下面的鏈接,(手冊(cè)還是要讀?。?/p>

https://docs.mongodb.com/v3.2/reference/method/js-user-management/

記得把配置文件的驗(yàn)證打開,重啟

yaml配置信息

server:"%mongodb_server%"

? ? ? ? ? ? options:

? ? ? ? ? ? ? ? username:"root"

? ? ? ? ? ? ? ? password:"root123"

? ? ? ? ? ? ? ? authSource: admin

如果使用URI配置應(yīng)該是

mongodb://root:root123@localhost:27017/?authSource=admin

至于這里面的authSource這個(gè)東西需要詳細(xì)了解下不然弄不懂這里的設(shè)置

單獨(dú)數(shù)據(jù)庫(kù)單獨(dú)配置賬號(hào)密碼后期再爬坑



symfony配置MongoDB先不寫:后期補(bǔ)

關(guān)于ORM Entity?和 ODM?Document同時(shí)使用,還在嘗試,希望不沖突,畢竟console那一套挺好用



關(guān)于字段的annotation注釋

Id主鍵的可選項(xiàng)如下

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.

文檔中對(duì)應(yīng)的代碼

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;


參考地址

https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/basic-mapping.html#basic-mapping


字段常用屬性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


關(guān)于一對(duì)一,一對(duì)多,多對(duì)一,多對(duì)多

分兩種 Reference?和?Embedded?兩種情況討論

Reference?屬于只引用的形式

Embedded?屬于直接關(guān)聯(lián),看文檔好像是說 Document(子文檔)是不能獨(dú)立存在的,

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容