1,下载MongoDB
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.4.tgz
2,安装MongoDB
tar zxf mongodb-linux-x86_64-4.0.4.tgz
mkdir -p /opt/mongodb
mv mongodb-linux-x86_64-4.0.4/* /opt/mongodb/
3,添加环境变量
cat >>/etc/profile<<\EOF
PATH=/opt/mongodb/bin:$PATH
export PATH
EOF
source /etc/profile
4,配置MongoDB
mkdir -p /opt/mongodb/{data,logs}
cat >/opt/mongodb/mongodb.conf<<\EOF
port=27017
dbpath=/opt/mongodb/data
logpath=/opt/mongodb/logs/mongodb.log
logappend=true
fork=true
pidfilepath=/opt/mongodb/mongod.pid
oplogSize=4096
maxConns=640000
directoryperdb=true
bind_ip=0.0.0.0
replSet=rs0
EOF
5,启动mongodb服务
mongod
./bin/mongod --dbpath=/opt/mongodb/data --logpath=/opt/mongodb/logs/mongodb.log --logappend --fork
6,登录mongo
mongo
> show databases
admin 0.000GB
config 0.000GB
local 0.000GB
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> use test
switched to db test
> db.createCollection("t1")
{ "ok" : 1 }
> show tables
t1
> show collections
t1
> db.t1.insert({"name":"xiaoleizi","age":18})
WriteResult({ "nInserted" : 1 })
> db.t1.find()
{ "_id" : ObjectId("5f2d5a4b95dac58b1b4b2b46"), "name" : "xiaoleizi", "age" : 18 }
db.createUser(
{
user: "root",
pwd: "123456",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
db.createUser({user:"root",pwd:"root",roles: [ { role:"root", db:"admin"}] })
|