安装MongoDB的方法有很多种,可以源代码安装,在Centos也可以用yum源安装的方法。
1、准备工作
运行yum命令查看MongoDB的包信息(正常是没有信息提示的,我这里已经按安装好了)
[[email protected]~]# yum info mongodb-org Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile Installed Packages Name : mongodb-org Arch : x86_64 Version : 3.4.10 Release : 1.el6 Size : 0.0 Repo : installed From repo : mongodb-org-3.4 Summary : MongoDB open source document-oriented database system (metapackage) URL : http://www.mongodb.org License : AGPL 3.0 Description : MongoDB is built for scalability, performance and high availability, scaling from single server deployments to large, complex multi-site architectures. By leveraging in-memory : computing, MongoDB provides high performance for both reads and writes. MongoDB’s native replication and automated failover enable enterprise-grade reliability and operational : flexibility. : : MongoDB is an open-source database used by companies of all sizes, across all industries and for a wide variety of applications. It is an agile database that allows schemas to : change quickly as applications evolve, while still providing the functionality developers expect from traditional databases, such as secondary indexes, a full query language : and strict consistency. : : MongoDB has a rich client ecosystem including hadoop integration, officially supported drivers for 10 programming languages and environments, as well as 40 drivers supported : by the user community. : : MongoDB features: : * JSON Data Model with Dynamic Schemas : * Auto-Sharding for Horizontal Scalability : * Built-In Replication for High Availability : * Rich Secondary Indexes, including geospatial : * TTL indexes : * Text Search : * Aggregation Framework & Native MapReduce : : This metapackage will install the mongo shell, import/export tools, other client utilities, server software, default configuration, and init.d scripts.
(如果没有提示相关匹配的信息,) 说明你的centos系统中的yum源不包含MongoDB的相关资源,
2、增加源
vim /etc/yum.repos.d/mongodb-3.4.repo
输入下面的语句:
[mongodb-org-3.4] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc
3、安装
yum install -y mongodb-org
4、启动Mongodb
service mongod start
以后有更新了执行yum update mongodb-org 即可。
5、服务器配置
# mongod.conf #where to log logpath=/data/logs/mongodb/mongod.log logappend=true #以追加方式写入日志 # fork and run in background fork=true #port=27017 dbpath=/data/mongo #数据库文件保存位置 # location of pidfile pidfilepath=/var/run/mongodb/mongod.pid # Listen to local interface only. Comment out to listen on all interfaces. bind_ip=10.81.85.229 # Disables write-ahead journaling # nojournal=true #启用定期记录CPU利用率和 I/O 等待 # Enables periodic logging of CPU utilization and I/O wait #cpu=true # 是否以安全认证方式运行,默认是不认证的非安全方式 # Turn on/off security. Off is currently the default #noauth=true auth=true # Verbose logging output. #verbose=true # Inspect all client data for validity on receipt (useful for # developing drivers) #objcheck=true # Enable db quota management 启用数据库配额管理,默认每个db可以有8个文件,可以用quotaFiles参数设置 #quota=true # Set oplogging level where n is # 0=off (default) # 1=W # 2=R # 3=both # 7=W+some reads #diaglog=0 # Ignore query hints #nohints=true # Enable the HTTP interface (Defaults to port 28017). #httpinterface=true # Turns off server-side scripting. This will result in greatly limited # functionality #noscripting=true # Turns off table scans. Any query that would do a table scan fails. #notablescan=true # Disable data file preallocation. #noprealloc=true # Specify .ns file size for new databases. # nssize=<size> # Replication Options # in replicated mongo databases, specify the replica set name here #replSet=setname # maximum size in megabytes for replication operation log #oplogSize=1024 # path to a key file storing authentication info for connections # between replica set members #keyFile=/path/to/keyfile
配置授权登录
> use admin > db.createUser({user:"root",pwd:"123456",roles:[{role:"root",db:"admin"}]})
登录数据库
mongo 127.0.0.1:27017/admin -u root -p
查询
> db.comlogs.find().count() 326466
PHP测试代码
<?php $mongo = new Mongo("mongodb://root:[email protected]:27017/admin"); //认证用户,这里的数据库,只启认证作用 $db = $mongo->selectDB(‘admin‘); //选取数据库 $users= $db->selectCollection("test"); $cursor = $users->find(); foreach ($cursor as $id => $value) { echo "$id: "; print_r($value); echo "<br>"; } $document = array( "title" => "MongoDB", "description" => "database", "likes" => 100, "url" => "http://www.cnblogs.com/chenpingzhao" ); $users->insert($document);
6、mongodb角色
内置角色
- 数据库用户角色:read、readWrite;
- 数据库管理角色:dbAdmin、dbOwner、userAdmin;
- 集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager;
- 备份恢复角色:backup、restore;
- 所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
- 超级用户角色:root, 这里还有几个角色间接或直接提供了系统超级用户的访问(dbOwner 、userAdmin、userAdminAnyDatabase)
- 内部角色:__system
具体角色
- read:允许用户读取指定数据库
- readWrite:允许用户读写指定数据库
- dbAdmin:允许用户在指定数据库中执行管理函数,如索引创建、删除,查看统计或访问system.profile
- userAdmin:允许用户向system.users集合写入,可以找指定数据库里创建、删除和管理用户
- clusterAdmin:只在admin数据库中可用,赋予用户所有分片和复制集相关函数的管理权限。
- readAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读权限
- readWriteAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读写权限
- userAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的userAdmin权限
- dbAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的dbAdmin权限。
- root:只在admin数据库中可用。超级账号,超级权限
时间: 2024-10-11 06:54:47