问题描述
我尝试将laravel网站与Mondo DB地图集集群连接.以前,我将此数据库与mysql数据库连接.但是我出错了
I try to connect my laravel website with Mondo DB atlas cluster. Previously I connect this database with mysql database. But I go an error
我在config/database.php上的代码
my code on config/database.php
'mongodb_conn' => [
'driver' => 'mongodb',
'dsn'=>'mongodb+srv://myusername:mypassword@mydbname-ktp6o.mongodb.net/test?retryWrites=true&w=majority',
'database' => 'mydbname',
],
还有我在.env中的代码
And my code in .env
APP_NAME=Blood_Donation
APP_ENV=local
APP_KEY=base64:jtvoXduyfwnIB6A5pCy8Cfa9bJYRvNgMKeRFdfiYYyc=
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
DB_CONNECTION=mongodb_conn'
DB_HOST=xxxx-shard-00-00-uggj0.mongodb.net
DB_PORT=27017
DB_DATABASE=mydbname
DB_USERNAME=myusername
DB_PASSWORD=mypassword
BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
运行PHP工匠服务时出现错误消息.
I got below error when I run PHP artisan serve.
推荐答案
如果您将XAMPP用于Localhost(Apache服务器)和MongoDB Atlas作为数据库,则应参考此 Jenssegers .
If you are using XAMPP for Localhost(Apache Server) and MongoDB Atlas as the Database you should refer this Jenssegers.
$ composer require jenssegers/mongodb
,并确保将 MongoDB扩展安装到XAMPP XAMPP + MongoDB配置 (执行第4步)
and make sure to install MongoDB extension to XAMPP XAMPP + MongoDB Configuration(Do Until Step 4)
DB_CONNECTION=mongodb
DB_HOST=xxxx-shard-00-00-uggj0.mongodb.net
DB_PORT=27017
DB_DATABASE={your_database}
DB_USERNAME={username_when_creating_cluster}
DB_PASSWORD={password_when_creating_cluster}
config/database.php
'mongodb' => [
'driver' => 'mongodb',
'dsn' => 'mongodb+srv://{username_when_creating_cluster}:{password_when_creating_cluster}@{your_cluster_name}.bhtvj.gcp.mongodb.net/{your_database}?retryWrites=true&w=majority',
'database' => '{your_database}',
],
请注意,上面的 dsn 可以从 MongoDB | Atlas 轻松获得您只应编辑集群和数据库名称的凭据.
Please note that above dsn can easily get from MongoDB|Atlas and you should only edit credential for the cluster and the database name.
希望这是您寻找Goodluck的答案!
Hope this is the answer you looking for Goodluck!
这篇关于如何将Laravel网站与mongoDB地图集连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!