解压mongodb-linux-x86_64-rhel70-4.0.6.tgzlinux
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下mongodb
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
建立数据库及用户json
> 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 app