linux下mongodb安装

解压mongodb-linux-x86_64-rhel70-4.0.6.tgz

tar -zxf  mongodb-linux-x86_64-rhel70-4.0.6.tgz -C /usr/local
mv mongodb-linux-x86_64-rhel70-4.0.6 mongodb

创建data log目录在home/data下

mkdir -p /home/data/mongodb/data
mkdir -p /home/data/mongodb/logs
touch /home/data/mongodb/logs/mongodb.log

新建配置文件mongodb.conf

touch /usr/local/mongodb/bin/mongodb.conf
[root@localhost bin]# cat mongodb.conf
dbpath=/home/data/mongodb/data
logpath=/home/data/mongodb/logs/mongodb.log
port=27017
#自己的ip
bind_ip= 192.168.1.175
#bind_ip_all=true
#bindIp: [127.0.0.1, 192.168.1.117]
#以追加的方式记录日志
logappend=true
#以后台方式运行进程
fork=true
#开启用户认证
auth=true
#启用日志文件,默认启用
journal=true
#这个选项可以过滤掉一些无用的日志信息,若需要调试使用请设置为false
quiet=true

创建数据库及用户

> use admin
> db.createUser( {user: "root",pwd: "password",roles: [{ role: "userAdminAnyDatabase", db: "admin" }]})

> use newdb;
switched to db newdb
> db.createUser({ user: "newuser",pwd: "password111",roles:[{ role:"readWrite",db: "newdb" }]}) 

> db.auth("newuser","password111")
> use admin
> db.auth("root","password")
1
#常用的命令
  >use test
switched to db test
> show collections
mycol
mycollection
newcollection
删除库 将删除当前所选数据库。 如果没有选择任何数据库,那么它将删除默认的’test‘数据库。
>db.dropDatabase()
Shell

查询某个数据库下的用户
>db.system.users.find() 

删除某个数据库下的所有用户
>db.system.users.remove()

删除指定用户
>db.system.users.remove({'user':'用户名'})

查看所有帐号
>  use admin
switched to db admin
> db.auth('dba','dba')
1
> db.system.users.find().pretty()
> db.system.users.find().count()
#删除名称为 mycollection 的集合。
>db.mycollection.drop()
true

> > show dbs
> show collections
> db.system.users.find()
> show users
    mongoexport -d dbname -c collectionname -o file --type json/csv -f field
        参数说明:
            -d :数据库名
            -c :collection名
            -o :输出的文件名
            --type : 输出的格式,默认为json
            -f :输出的字段,如果-type为csv,则需要加上-f "字段名"
    mongoimport -d dbname -c collectionname --file filename --headerline --type json/csv -f field
        参数说明:
            -d :数据库名
            -c :collection名
            --type :导入的格式默认json
            -f :导入的字段名
            --headerline :如果导入的格式是csv,则可以使用第一行的标题作为导入的字段
            --file :要导入的文件

例:导出导入全部数据
  ./mongodump -h 192.168.1.179:27017 -d testdb -utestuser -ppassword -o /usr/local/mongodb/bin/newdb.dmp

  ./mongorestore -h 192.168.1.175:27017 -d testdb -utestuser -ppassword /usr/local/mongodb/bin/newdb.dmp/new

导出导入集合
  ./mongodump -h 192.168.1.179:27017 -d testdb -utestuser -ppassword -c test_resource -o /usr/local/mongodb/bin/test_resource.dmp

  ./mongorestore -h 192.168.1.175:27017 -d testdb -utestuser -ppassword -c test_resource /usr/local/mongodb/bin/sys_resource.dmp/new/test_resource.bson 

停服务
./mongod -shutdown -dbpath=/home/data/mongodb/data
启动
./mongod -f /usr/local/mongodb/bin/mongodb.conf

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章