美文网首页
mongo安装维护

mongo安装维护

作者: 简书她姥爷 | 来源:发表于2019-04-19 09:03 被阅读0次

    该文档默认命名和配置:

    1. 复制集名称为:ulocation
    2. 三台mongo实例,192.168.50.222(primary,第一台),192.168.50.223(secondary,第二胎),192.168.50.223(arbiter,第三台)
    3. mongo数据目录:/var/lib/mongo/,日志目录/var/log/mongodb/,备份目录:/app/mongo/backup,keyfile文件:/app/mongo/mongo.keyfile

    mongo centos安装卸载

    注: 如下操作是在ip、防火墙、selinux都配置好后进行的。

    1.配置yum源

    vim /etc/yum.repos.d/mongodb-enterprise.repo 
    
    [mongodb-enterprise]
    name=MongoDB Enterprise Repository
    baseurl=https://repo.mongodb.com/yum/redhat/$releasever/mongodb-enterprise/4.0/$basearch/
    gpgcheck=1
    enabled=1
    gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
    

    2.yum安装

    yum install -y mongodb-enterprise-4.0.6 mongodb-enterprise-server-4.0.6 mongodb-enterprise-shell-4.0.6 mongodb-enterprise-mongos-4.0.6 mongodb-enterprise-tools-4.0.6
    

    3.防止yum update更新

    vim /etc/yum.conf 
    exclude=mongodb-enterprise,mongodb-enterprise-server,mongodb-enterprise-shell,mongodb-enterprise-mongos,mongodb-enterprise-tools
    

    4.常用命令

    sudo service mongod start
    sudo chkconfig mongod on
    sudo service mongod stop
    sudo service mongod restart
    

    5.mongo centos移除

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

    以上5部骤参考:https://docs.mongodb.com/manual/tutorial/install-mongodb-enterprise-on-red-hat/

    6.Percona Server for MongoDB 4.0 安装

    yum install https://repo.percona.com/yum/percona-release-latest.noarch.rpm
    
    percona-release enable psmdb-40 release
    yum install percona-server-mongodb
    
    service mongod start
    chkconfig mongod on
    service mongod stop
    

    mongo开启身份认证(复制集环境)

    https://docs.mongodb.com/manual/tutorial/enable-authentication/

    创建keyfile

    mkdir -p /app/mongo
    openssl rand -base64 756 > /app/mongo/mongo.keyfile
    chmod 400 /app/mongo/mongo.keyfile
    chown mongod:mongod /app/mongo/mongo.keyfile�
    

    创建超级管理员

    mongo
    use admin
    
    db.createUser(
      {
        user: "admin",
        pwd: "admin@13579*01",
        roles: [ { role: "userAdminAnyDatabase", db: "admin" }, { "role" : "clusterAdmin", "db" : "admin" }, "readWriteAnyDatabase","root" ]
      }
    )
    

    创建ulocation管理员

    use ulocation
    db.createUser(
      {
        user: "ulocation",
        pwd: "ulocation2019",
        roles: [ { role: "readWrite", db: "ulocation" }]
      }
    )
    

    若为复制集,需将keyfile拷贝到其他节点上。

    关闭mongo实例,修改配置文件:

    security:
      authorization: enabled
      keyFile: /app/mongo/mongo.keyfile�
    

    使用创建的用户登录

    mongo --port 27017 -u "admin" --authenticationDatabase "admin" -p
    
    mongo --port 27017 -u "ulocation" --authenticationDatabase "ulocation" -p
    --authenticationDatabase : 指定用户在哪个库中创建,若不指定,mongo使用连接串中的数据库。
    

    Standalone mongo实例转为复制集

    1.关闭第一台实例

    mongo
    use admin
    db.shutdownServer()
    

    2.第一台实例增加复制集启动配置项,并启动

    vim /etc/mongod.conf
    
    replication:
      replSetName: ulocation
      enableMajorityReadConcern: false   # PSA架构需关闭此项
    
    service mongod start
    

    3.第一台实例配置复制集信息

    mongo
    config = {
        "_id":"ulocation",
        "members":[
            {"_id":0,"host":"192.168.50.222:27017"},
        ]
    }
    rs.initiate(config)
    
    该节点为primary节点
    

    4.新增secondary mongo

    在第二台mongo实例,操作1、2两部
    
    在primary上执行:
    rs.add( { host: "192.168.50.223:27017", priority: 0, votes: 0 } )
    调用该命令后,会自动执行Initial Sync,进行数据同步
    
    修改priority和vote(原因:When a newly added secondary has its votes and priority settings greater than zero, during its initial sync, the secondary still counts as a voting member even though it cannot serve reads nor become primary because its data is not yet consistent.)
    var cfg = rs.conf();
    cfg.members[1].priority = 1
    cfg.members[1].votes = 1
    rs.reconfig(cfg)
    

    5.新增仲裁成员

    在第三台mongo实例,操作1、2两部
    在primary上执行:
    rs.addArb("192.168.50.224:27017")
    

    参考:https://docs.mongodb.com/manual/tutorial/convert-standalone-to-replica-set/

    https://docs.mongodb.com/manual/tutorial/expand-replica-set/

    https://docs.mongodb.com/manual/tutorial/add-replica-set-arbiter/

    Initial Sync数据同步

    两种方式:

    方式一:在primary上执行:rs.add( { host: "192.168.50.223:27017", priority: 0, votes: 0 } )。注:前提是空的数据目录

    方式二:拷贝primary上的数据文件,步骤如下:

    1.停掉primary,拷贝数据文件
    2.停掉secondary将拷贝的数据文件移到secondary的数据目录
    3.rm -rf mongod.lock(不删除会报“Attempted to create a lock file on a read-only directory: /var/lib/mongo”)
    chown -R mongod:mongod *
    4.启动secondary上的mongo实例
    

    Percona Server热备份

    mkdir -p /app/mongo/backup
    chown -R mongod:mongod /app/mongo/backup
    
    > use admin
    switched to db admin
    > db.runCommand({createBackup: 1, backupDir: "/app/mongo/backup"})
    { "ok" : 1 }
    

    mongo shell常用语句

    库操作:

    show dbs;
    use test;
    test.dropDatabase();
    

    查询语句:

    日期:ISODate("2012-12-19T06:01:17.171Z")
    db.inventory.find( {} )
    db.inventory.find( { status: "D" } )
    db.inventory.find( { status: { $in: [ "A", "D" ] } } )
    db.inventory.find( { status: "A", qty: { $lt: 30 } } )
    db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } )
    

    赋角色:

    db.grantRolesToUser(
        "dayunAdmin",
        [
          { role: "root", db: "admin" }
        ]
    )   
    

    复制集

    rs.conf()
    rs.status()
    db.printReplicationInfo()
    rs.printSlaveReplicationInfo()
    

    FAQ

    • (PSA) architecture关闭enableMajorityReadConcern

      Read Concern "majority": https://docs.mongodb.com/manual/reference/read-concern-majority/#disable-read-concern-majority

    • 主节点db.shutdownServer(),会出现失败

        ulocation:PRIMARY> db.shutdownServer()
        2019-04-17T12:34:12.606+0800 E QUERY    [js] Error: shutdownServer failed: {
            "operationTime" : Timestamp(1555475638, 1),
            "ok" : 0,
            "errmsg" : "No electable secondaries caught up as of 2019-04-17T12:34:12.604+0800Please use the replSetStepDown command with the argument {force: true} to force node to step down.",
            "code" : 262,
            "codeName" : "ExceededTimeLimit",
            "$clusterTime" : {
                "clusterTime" : Timestamp(1555475638, 1),
                "signature" : {
                    "hash" : BinData(0,"dHvSJ4hy78YRfqf0hSAotb29wuo="),
                    "keyId" : NumberLong("6680450114256896002")
                }
            }
        } :
      

      解决:先降级,db.adminCommand( { replSetStepDown: 120, secondaryCatchUpPeriodSecs: 15, force: true } ),然后,关闭。

    • 先正常运行,修改配置后运行不成功,还原回去后还不能正常运行

      删除 /tmp/mongodb-27017.sock

    相关文章

      网友评论

          本文标题:mongo安装维护

          本文链接:https://www.haomeiwen.com/subject/ccvfgqtx.html