• centos7下mongodb安装及开启副本


    centos7下mongodb安装及开启副本

    下载安装包

    https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.13.tgz

    解压并修改目录名称

    mongodb-4.0.13

    编写配置文件

    dbpath=/data/mongodb/db
    bind_ip=0.0.0.0
    port=27017
    logpath=/data/mongodb/logs/mongodb.log
    logappend=true
    auth=false
    replSet=rs0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    保存为mongodb.conf文件

    设置mongodb的环境变量

    export MONGODB_HOME=/opt/mongodb-4.0.13
    export PATH=$PATH:$MONGODB_HOME/bin
    
    • 1
    • 2
    source /etc/profile
    
    • 1

    启动mongodb

    mongod -f conf/mongodb.conf
    
    • 1

    添加用户及权限的脚本

    HOSTNAME=`hostname`
    
      OPTS=`getopt -o h: --long hostname: -n 'parse-options' -- "$@"`
      if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; exit 1 ; fi
    
      echo "$OPTS"
      eval set -- "$OPTS"
    
      while true; do
        case "$1" in
          -h | --hostname )     HOSTNAME=$2;        shift; shift ;;
          -- ) shift; break ;;
          * ) break ;;
        esac
      done
    echo "Using HOSTNAME='$HOSTNAME'"
    
    mongo localhost:27017/inventory <<-EOF
        rs.initiate({
            _id: "rs0",
            members: [ { _id: 0, host: "${HOSTNAME}:27017" } ]
        });
    EOF
    echo "Initiated replica set"
    
    sleep 3
    mongo localhost:27017/admin <<-EOF
        db.createUser({ user: 'admin', pwd: 'admin', roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] });
    EOF
    
    mongo -u admin -p admin localhost:27017/admin <<-EOF
        db.runCommand({
            createRole: "listDatabases",
            privileges: [
                { resource: { cluster : true }, actions: ["listDatabases"]}
            ],
            roles: []
        });
    
        db.createUser({
            user: 'debezium',
            pwd: 'dbz',
            roles: [
                { role: "readWrite", db: "inventory" },
                { role: "read", db: "local" },
                { role: "listDatabases", db: "admin" },
                { role: "read", db: "config" },
                { role: "read", db: "admin" }
            ]
        });
    EOF
    
    echo "Created users"
    
    mongo -u debezium -p dbz --authenticationDatabase admin localhost:27017/inventory <<-EOF
        use inventory;
    
        db.products.insert([
            { _id : NumberLong("101"), name : 'scooter', description: 'Small 2-wheel scooter', weight : 3.14, quantity : NumberInt("3") },
            { _id : NumberLong("102"), name : 'car battery', description: '12V car battery', weight : 8.1, quantity : NumberInt("8") },
            { _id : NumberLong("103"), name : '12-pack drill bits', description: '12-pack of drill bits with sizes ranging from #40 to #3', weight : 0.8, quantity : NumberInt("18") },
            { _id : NumberLong("104"), name : 'hammer', description: "12oz carpenter's hammer", weight : 0.75, quantity : NumberInt("4") },
            { _id : NumberLong("105"), name : 'hammer', description: "14oz carpenter's hammer", weight : 0.875, quantity : NumberInt("5") },
            { _id : NumberLong("106"), name : 'hammer', description: "16oz carpenter's hammer", weight : 1.0, quantity : NumberInt("0") },
            { _id : NumberLong("107"), name : 'rocks', description: 'box of assorted rocks', weight : 5.3, quantity : NumberInt("44") },
            { _id : NumberLong("108"), name : 'jacket', description: 'water resistent black wind breaker', weight : 0.1, quantity : NumberInt("2") },
            { _id : NumberLong("109"), name : 'spare tire', description: '24 inch spare tire', weight : 22.2, quantity : NumberInt("5") }
        ]);
    
        db.customers.insert([
            { _id : NumberLong("1001"), first_name : 'Sally', last_name : 'Thomas', email : 'sally.thomas@acme.com' },
            { _id : NumberLong("1002"), first_name : 'George', last_name : 'Bailey', email : 'gbailey@foobar.com' },
            { _id : NumberLong("1003"), first_name : 'Edward', last_name : 'Walker', email : 'ed@walker.com' },
            { _id : NumberLong("1004"), first_name : 'Anne', last_name : 'Kretchmar', email : 'annek@noanswer.org' }
        ]);
    
        db.orders.insert([
            { _id : NumberLong("10001"), order_date : new ISODate("2016-01-16T00:00:00Z"), purchaser_id : NumberLong("1001"), quantity : NumberInt("1"), product_id : NumberLong("102") },
            { _id : NumberLong("10002"), order_date : new ISODate("2016-01-17T00:00:00Z"), purchaser_id : NumberLong("1002"), quantity : NumberInt("2"), product_id : NumberLong("105") },
            { _id : NumberLong("10003"), order_date : new ISODate("2016-02-19T00:00:00Z"), purchaser_id : NumberLong("1002"), quantity : NumberInt("2"), product_id : NumberLong("106") },
            { _id : NumberLong("10004"), order_date : new ISODate("2016-02-21T00:00:00Z"), purchaser_id : NumberLong("1003"), quantity : NumberInt("1"), product_id : NumberLong("107") }
        ]);
    EOF
    
    echo "Inserted example data"
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86

    保存为sh后缀的脚本,执行此文件,此时mongodb的inventory库可做实时的cdc处理了

    参考资料

  • 相关阅读:
    axios 用formData的方式请求数据
    信贷产品额度定价场景下的回归模型效果评估
    Python爬虫批量下载图片
    日志导致线程Block的这些坑,你不得不防
    独立站SEO分析工具
    [物联网] OneNet 多协议TCP透传
    【深入设计模式】建造者模式—带你彻底弄懂建造者模式
    实施电子采购没思路?数商云助企业采购全流程高效协同
    [数据集][目标检测]高空抛物数据集VOC+YOLO格式259张+6段视频+yolov8模型+探讨
    安装与配置FTP服务器
  • 原文地址:https://blog.csdn.net/w757227129/article/details/126117809