• 欢迎访问金笔头博客,这是一个菜鸟(伪)程序员的自留地,欢迎访问我的github:点击进入

Centos7上MongoDB的安装和配置

数据库 eason 1944次浏览 0个评论 扫描二维码

新建用户和用户组

$ sudo groupadd mongodb
$ sudo useradd -g mongodb mongodb
$ sudo passwd mongodb 
更改用户 mongodb 的密码 。
新的 密码:
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。

下载并解压mongodb

$ cd /opt/
$ sudo mkdir -p db/mongodb
$ cd db/mongodb/
$ sudo wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.4.2.tgz
$ sudo tar -zxvf mongodb-linux-x86_64-rhel70-3.4.2.tgz
$ sudo rm mongodb-linux-x86_64-rhel70-3.4.2.tgz

配置环境变量

export PATH=/opt/db/mongodb/mongodb-linux-x86_64-rhel70-3.4.2/bin:$PATH

创建数据和日志存放目录

$ sudo mkdir -p /opt/db/mongodb/data
$ sudo mkdir -p /opt/db/mongodb/logs

创建并编辑配置文件

$ sudo touch mongo.conf
$ sudo vi mongo.conf

配置文件内容如下:

dbpath = /opt/db/mongodb/data#数据文件存放目录 logpath = /opt/db/mongodb/logs/mongodb.log#日志文件存放目录 port = 12345#端口 fork = true#以守护程序的方式启用,即在后台运行 nohttpinterface = true

更改文件夹属主

$ sudo chown -R mongodb:mongodb /opt/db/mongodb/

运行

$ su mongodb  #切换到mongodb用户
$ mongod -f /opt/db/mongodb/mongo.conf  #指定配置文件并运行
about to fork child process, waiting until server is ready for connections.
forked process: 8574
child process started successfully, parent exiting

检查是否启动成功

$ netstat -lanp | grep "12345"
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 0.0.0.0:12345           0.0.0.0:*               LISTEN      9162/mongod         
unix  2      [ ACC ]     STREAM     LISTENING     87082    9162/mongod          /tmp/mongodb-12345.sock

可以看到,已经启动成功。

连接

使用命令mongo [ip:port]连接mongodb

$ mongo 127.0.0.1:12345
MongoDB shell version v3.4.2
connecting to: 127.0.0.1:12345
MongoDB server version: 3.4.2
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
    http://docs.mongodb.org/
Questions? Try the support group
    http://groups.google.com/group/mongodb-user
Server has startup warnings: 
2017-03-21T21:17:43.489+0800 I CONTROL  [initandlisten] 
2017-03-21T21:17:43.489+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-03-21T21:17:43.489+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-03-21T21:17:43.489+0800 I CONTROL  [initandlisten] 
2017-03-21T21:17:43.490+0800 I CONTROL  [initandlisten] 
2017-03-21T21:17:43.490+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-03-21T21:17:43.490+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-03-21T21:17:43.490+0800 I CONTROL  [initandlisten] 
2017-03-21T21:17:43.490+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-03-21T21:17:43.490+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-03-21T21:17:43.490+0800 I CONTROL  [initandlisten] 
> db.help()
DB methods:
    db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [ just calls db.runCommand(...) ]
    db.auth(username, password)
    db.cloneDatabase(fromhost)
    db.commandHelp(name) returns the help for the command
    db.copyDatabase(fromdb, todb, fromhost)
    db.createCollection(name, { size : ..., capped : ..., max : ... } )
    db.createView(name, viewOn, [ { $operator: {...}}, ... ], { viewOptions } )
    db.createUser(userDocument)
    db.currentOp() displays currently executing operations in the db
    db.dropDatabase()
    db.eval() - deprecated
    db.fsyncLock() flush data to disk and lock server for backups
    db.fsyncUnlock() unlocks server following a db.fsyncLock()
    db.getCollection(cname) same as db['cname'] or db.cname
    db.getCollectionInfos([filter]) - returns a list that contains the names and options of the db's collections
    db.getCollectionNames()
    db.getLastError() - just returns the err msg string
    db.getLastErrorObj() - return full status object
    db.getLogComponents()
    db.getMongo() get the server connection object
    db.getMongo().setSlaveOk() allow queries on a replication slave server
    db.getName()
    db.getPrevError()
    db.getProfilingLevel() - deprecated
    db.getProfilingStatus() - returns if profiling is on and slow threshold
    db.getReplicationInfo()
    db.getSiblingDB(name) get the db at the same server as this one
    db.getWriteConcern() - returns the write concern used for any operations on this db, inherited from server object if set
    db.hostInfo() get details about the server's host
    db.isMaster() check replica primary status
    db.killOp(opid) kills the current operation in the db
    db.listCommands() lists all the db commands
    db.loadServerScripts() loads all the scripts in db.system.js
    db.logout()
    db.printCollectionStats()
    db.printReplicationInfo()
    db.printShardingStatus()
    db.printSlaveReplicationInfo()
    db.dropUser(username)
    db.repairDatabase()
    db.resetError()
    db.runCommand(cmdObj) run a database command.  if cmdObj is a string, turns it into { cmdObj : 1 }
    db.serverStatus()
    db.setLogLevel(level,<component>)
    db.setProfilingLevel(level,<slowms>) 0=off 1=slow 2=all
    db.setWriteConcern( <write concern doc> ) - sets the write concern for writes to the db
    db.unsetWriteConcern( <write concern doc> ) - unsets the write concern for writes to the db
    db.setVerboseShell(flag) display extra information in shell output
    db.shutdownServer()
    db.stats()
    db.version() current version of the server
>

设置用户名和密码

> db.createUser({user:"jbt",pwd:"jinbitou",roles:["readWrite","dbAdmin"]})
Successfully added user: { "user" : "jbt", "roles" : [ "readWrite", "dbAdmin" ] }
>

启用密码验证

编辑配置文件mongo.conf

$ vi /opt/db/mongodb/mongo.conf

添加如下行

auth = true

保存并退出

登录验证

首先需要使用use dbName,如: use admin跳转到当前数据库模式下,然后登录

$ mongod -f mongodb/mongo.conf
about to fork child process, waiting until server is ready for connections.
forked process: 10097
child process started successfully, parent exiting
$ mongo 127.0.0.1:12345
MongoDB shell version v3.4.2
connecting to: 127.0.0.1:12345
MongoDB server version: 3.4.2
> use admin
switched to db admin
> db.auth("jbt","jinbitou")
1
>

上面1表示登录成功,0表示登录失败。

停止

$ mongod -f /opt/db/mongodb/mongo.conf --shutdown
2017-03-21T21:09:44.885+0800 I CONTROL  [main] log file "/opt/db/mongodb/logs/mongodb.log" exists; moved to "/opt/db/mongodb/logs/mongodb.log.2017-03-21T13-09-44".
killing process with pid: 8574

金笔头博客, 版权所有丨如未注明 , 均为原创, 转载请注明Centos7上MongoDB的安装和配置
喜欢 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址