mongodb安装

来源:互联网 发布:中铁物资西南公司知乎 编辑:程序博客网 时间:2024/06/05 17:42
下载:
curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz 
这种方式并不成功。curl: (35) error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure
然后使用浏览器直接下载:
https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel55-3.2.5.tgz
解压:
tar -zxvf mongodb-linux-x86_64-rhel55-3.2.5.tgz 
mv mongodb-linux-x86_64-rhel55-3.2.5  /usr/local/mongodb
可以配置一下环境变量:
export PATH=<mongodb-install-directory>/bin:$PATH

创建数据库目录:
mkdir -p /data/db

运行 mongodb服务
mongod
后台
mongo

实际中:
 su - mogodb -s /bin/bash
 /usr/local/mongodb/bin/mongod --bind_ip=127.0.0.1,192.168.1.3 --auth &
在启动时加上--norc参数,就可以禁止加载mongorc.js(貌似mongodb 3.2上已经没有了这个参数)

2016-04-20T17:33:47.504+0800 I CONTROL  [initandlisten] ** WARNING: Insecure configuration, access control is not enabled and no --bind_ip has been specified.
2016-04-20T17:33:47.504+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted,
2016-04-20T17:33:47.504+0800 I CONTROL  [initandlisten] **          and the server listens on all available network interfaces.
2016-04-20T17:33:47.504+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2016-04-20T17:33:47.504+0800 I CONTROL  [initandlisten]
2016-04-20T17:33:47.504+0800 I CONTROL  [initandlisten]
2016-04-20T17:33:47.504+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. The locked memory size is 32768 bytes, it should be at least 65536 bytes

解决rlimits问题:
/etc/security/limits.conf
*               soft    nofile   10240
*               hard    nofile   10240
*               soft    nproc    10240
*               hard    nproc    10240
*               soft    memlock  65536
*               hard    memlock    65536
注意:这里The locked memory size is 32768 bytes是指的hard的值,需要修改才能生效
其他问题分别通过启动项添加参数即可解决。
切换用户进行启动:The locked memory size is 32768 bytes是指的hard的默认值,必须修改hard才能生效

su - mogodb -s /bin/bash
 /usr/local/mongodb/bin/mongod --bind_ip=127.0.0.1,192.168.1.3 --auth &
查看启动进程:
 ps -ef|grep mongo
mogodb   20821     1  3 19:24 pts/1    00:00:01 /usr/local/mongodb/bin/mongod --bind_ip=127.0.0.1,192.168.1.3 --auth
root     20852 20223  0 19:25 pts/1    00:00:00 grep mongo

创建数据库及其它命令:
mongodb保留有几个:admin、local、config
mongodb 3.2版本安装完成之后,show dbs,默认会有一个local
admin需要新建,config也需要新建。当数据库中有数据时,show dbs才会显示该库(
======================================================
> use config
switched to db config
> show dbs
admin     0.000GB
linkeadb  0.000GB
local     0.000GB
test      0.000GB
> show roles
{
    "role" : "dbAdmin",
    "db" : "config",
    "isBuiltin" : true,
    "roles" : [ ],
    "inheritedRoles" : [ ]
}
=================================================================

创建一个自己的库test:
> use test
switched to db test
> db.createUser(
... {
... user:"lwx",
... pwd:"lwx123",
... roles:[{role:"readWrite",db:"test"}]
... }
... )
Successfully added user: {
    "user" : "lwx",
    "roles" : [
        {
            "role" : "readWrite",
            "db" : "test"
        }
    ]
}

这是admin库中的相关权限角色:
 show roles
{
    "role" : "__system",
    "db" : "admin",
    "isBuiltin" : true,
    "roles" : [ ],
    "inheritedRoles" : [ ]
}
{
    "role" : "backup",
    "db" : "admin",
    "isBuiltin" : true,
    "roles" : [ ],
    "inheritedRoles" : [ ]
}
{
    "role" : "clusterAdmin",
    "db" : "admin",
    "isBuiltin" : true,
    "roles" : [ ],
    "inheritedRoles" : [ ]
}
{
    "role" : "clusterManager",
    "db" : "admin",
    "isBuiltin" : true,
    "roles" : [ ],
    "inheritedRoles" : [ ]
}
{
    "role" : "clusterMonitor",
    "db" : "admin",
    "isBuiltin" : true,
    "roles" : [ ],
    "inheritedRoles" : [ ]
}
{
    "role" : "dbAdmin",
    "db" : "admin",
    "isBuiltin" : true,
    "roles" : [ ],
    "inheritedRoles" : [ ]
}
{
    "role" : "dbAdminAnyDatabase",
    "db" : "admin",
    "isBuiltin" : true,
    "roles" : [ ],
    "inheritedRoles" : [ ]
}
{
    "role" : "dbOwner",
    "db" : "admin",
    "isBuiltin" : true,
    "roles" : [ ],
    "inheritedRoles" : [ ]
}
{
    "role" : "enableSharding",
    "db" : "admin",
    "isBuiltin" : true,
    "roles" : [ ],
    "inheritedRoles" : [ ]
}
{
    "role" : "hostManager",
    "db" : "admin",
    "isBuiltin" : true,
    "roles" : [ ],
    "inheritedRoles" : [ ]
}
{
    "role" : "read",
    "db" : "admin",
    "isBuiltin" : true,
    "roles" : [ ],
    "inheritedRoles" : [ ]
}
{
    "role" : "readAnyDatabase",
    "db" : "admin",
    "isBuiltin" : true,
    "roles" : [ ],
    "inheritedRoles" : [ ]
}
{
    "role" : "readWrite",
    "db" : "admin",
    "isBuiltin" : true,
    "roles" : [ ],
    "inheritedRoles" : [ ]
}
{
    "role" : "readWriteAnyDatabase",
    "db" : "admin",
    "isBuiltin" : true,
    "roles" : [ ],
    "inheritedRoles" : [ ]
}
{
    "role" : "restore",
    "db" : "admin",
    "isBuiltin" : true,
    "roles" : [ ],
    "inheritedRoles" : [ ]
}
{
    "role" : "root",
    "db" : "admin",
    "isBuiltin" : true,
    "roles" : [ ],
    "inheritedRoles" : [ ]
}
{
    "role" : "userAdmin",
    "db" : "admin",
    "isBuiltin" : true,
    "roles" : [ ],
    "inheritedRoles" : [ ]
}
{
    "role" : "userAdminAnyDatabase",
    "db" : "admin",
    "isBuiltin" : true,
    "roles" : [ ],
    "inheritedRoles" : [ ]
}
config库中:
> show roles
{
    "role" : "dbAdmin",
    "db" : "config",
    "isBuiltin" : true,
    "roles" : [ ],
    "inheritedRoles" : [ ]
}

{
    "role" : "dbOwner",
    "db" : "config",
    "isBuiltin" : true,
    "roles" : [ ],
    "inheritedRoles" : [ ]
}
{
    "role" : "enableSharding",
    "db" : "config",
    "isBuiltin" : true,
    "roles" : [ ],
    "inheritedRoles" : [ ]
}
{
    "role" : "read",
    "db" : "config",
    "isBuiltin" : true,
    "roles" : [ ],
    "inheritedRoles" : [ ]
}
{
    "role" : "readWrite",
    "db" : "config",
    "isBuiltin" : true,
    "roles" : [ ],
    "inheritedRoles" : [ ]
}
{
    "role" : "userAdmin",
    "db" : "config",
    "isBuiltin" : true,
    "roles" : [ ],
    "inheritedRoles" : [ ]
}

修改用户角色:(参考https://docs.mongodb.org/manual/reference/method/db.updateUser/#db.updateUser
> db.updateUser( "linkadm",
... {
... roles:[
... {role:"dbAdmin",db:"linkeadb"},{role:"readWrite",db:"linkeadb"}
... ]
... }
... )
> db.linkeadb.insert({"test":"test"})
WriteResult({ "nInserted" : 1 })
要注意这个格式!

/usr/local/mongodb/bin/mongo
MongoDB shell version: 3.2.5
connecting to: test
2016-04-28T10:26:19.882+0800 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:48914 #1 (1 connection now open)
2016-04-28T10:26:19.884+0800 I ACCESS   [conn1] Unauthorized: not authorized on admin to execute command { getLog: "startupWarnings" }
2016-04-28T10:26:19.887+0800 I ACCESS   [conn1] Unauthorized: not authorized on admin to execute command { replSetGetStatus: 1.0, forShell: 1.0 }
> use linkeadb
switched to db linkeadb
> db.auth('linkadm','jf&*3.#K89')
2016-04-28T10:27:40.706+0800 I ACCESS   [conn1] Successfully authenticated as principal linkadm on linkeadb
1
2016-04-28T10:27:40.707+0800 I ACCESS   [conn1] Unauthorized: not authorized on admin to execute command { replSetGetStatus: 1.0, forShell: 1.0 }
> db.linkeadb.find()
{ "_id" : ObjectId("5720746156a7d91571e298fe"), "test" : "test" }
插入:
db.linkeadb.insert({"test","test"})

使用yum安装:
修改yum的源文件配置:(参考文章:
https://docs.mongodb.org/manual/tutorial/install-mongodb-on-red-hat/
http://www.liquidweb.com/kb/how-to-install-mongodb-on-centos-7/)
[mongodb-enterprise]
name=MongoDB Enterprise Repository
baseurl=https://repo.mongodb.com/yum/redhat/$releasever/mongodb-enterprise/stable/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc

yum install mongodb-org
可能需要的依赖包:
Version 5
yum install perl cyrus-sasl cyrus-sasl-plain cyrus-sasl-gssapi krb5-libs \            lm_sensors net-snmp openssl popt rpm-libs tcp_wrappers zlib
Version 6
yum install cyrus-sasl cyrus-sasl-plain cyrus-sasl-gssapi krb5-libs \            net-snmp openssl
Version 7
yum install cyrus-sasl cyrus-sasl-plain cyrus-sasl-gssapi krb5-libs \
            lm_sensors-libs net-snmp-agent-libs net-snmp openssl rpm-libs \
            tcp_wrappers-libs
另一个源:
[mongodb]
name=MongoDB Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/
gpgcheck=0
enabled=1

卸载mongodb
service mongod stop
yum erase $(rpm -qa | grep mongodb-org)
rm -r /var/log/mongodb
rm -r /var/lib/mongo

参考文档:
http://www.runoob.com/mongodb/mongodb-linux-install.html
https://docs.mongodb.org/manual/reference/configuration-options/

0 0
原创粉丝点击