Linux安装mongodb

  1. 作者QQ:67065435 QQ群:821635552

  2. 下载安装mongodb3.4.x

    cd ~
    wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.2.tgz
    tar -zxvf mongodb-linux-x86_64-3.4.2.tgz
    cp -r /root/mongodb-linux-x86_64-3.4.2 /usr/local/mongodb
    
  3. 加入环境变量

    vim /etc/profile
    export PATH=$PATH:/usr/local/mongodb/bin
    ESC
    :wq
    source /etc/profile
    
  4. 创建数据目录

    mkdir /data/
    mkdir /data/mongodb/
    mkdir /data/mongodb/logs/
    mkdir /data/mongodb/data/
    
  5. 修改配置文件

    vim /usr/local/mongodb/mongodb.conf
    
    fork = true
    port = 27017
    logappend=true
    nohttpinterface = false
    dbpath=/data/mongodb/data/
    logpath=/data/mongodb/logs/mongodb.log
    pidfilepath=/data/mongodb/logs/mongodb.pid
    
    ESC
    :wq
    
  6. 修改目录权限

    useradd mongodb
    chown -R mongodb:mongodb /data/mongodb
    chown -R mongodb:mongodb /usr/local/mongodb
    
  7. 开机启动mongodb

    cd /etc/systemd/system
    vim mongodb.service
    
    [Unit]
    Description=Start mongodb on boot.
    After=default.target network.target
    
    [Service]
    User=mongodb
    Group=mongodb
    Type=forking
    ExecStart=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongodb.conf
    PrivateTmp=true
    Restart=always
    
    [Install]
    WantedBy=multi-user.target
    
    ESC
    :wq
    
    systemctl daemon-reload
    systemctl enable mongodb
    systemctl start mongodb
    

Mongodb集群(cluster)

  1. 修改mongodb.conf

    vim /usr/local/mongodb/mongodb.conf
    
    fork=true
    port=27017
    logappend=true
    nounixsocket=false
    nohttpinterface=true
    dbpath=/data/mongodb/data/
    unixSocketPrefix=/data/mongodb/data/
    logpath=/data/mongodb/logs/mongodb.log
    pidfilepath=/data/mongodb/logs/mongodb.pid
    # 先查看本机IP($ ip addr),再绑定内网IP
    bind_ip=192.168.1.150
    replSet=Rs1/192.168.1.150:27017
    
    ESC
    :wq
    
  2. 启动关闭mongodb

    /usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongodb.conf
    /usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongodb.conf --shutdown
    
  3. 初始化mongodb,启动副本集

    # 查看副本集状态
    rs.status()
    
    # 1. 启动新副本集
    rs.initiate()
    
    # 2. 配置mongodb服务节点
    config_rs1={
        _id:'Rs1',members:[
            {_id:0,host:'192.168.1.149:27017',priority:1},
            {_id:1,host:'192.168.1.150:27017',priority:1}
        ]
    }
    
    # 显示当前簇
    rs.isMaster()
    
  4. 关闭mongodb

    /usr/local/mongodb-3.4.2/bin/mongod --shutdown --config /usr/local/mongodb-3.4.2/mongodb.conf
    
  5. Mongodb常用命令

    # 显示所有数据库
    show dbs;
    # 选中数据库
    use db_name;
    # 删除数据库
    db.dropDatabase();
    # 显示所有集合
    show collections;
    # 创建新的集合
    db.createCollection(COLLECTION_NAME, option);
    # 删除一个集合(COLLECTION_NAME)
    db.COLLECTION_NAME.drop();
    # 向集合插入数据
    db.COLLECTION_NAME.insert({name:"dog",age:2})
    # 删除数据
    db.COLLECTION_NAME.remove({})
    # 退出Mongodb
    quit;
    

MongoDB命令行

  1. 进入mongo控制台

     mongo --host [ip]
    
  2. 查看所有数据库

     show databases;
    
  3. 查看指定库下所有文档

     use database_name;
     show tables;
    
  4. 删除文档

     # 非集群方式下的删除
     db.table_name.remove({});
    
     # 集群(cluster)方式下的删除
     db["table_name"].remove({});
    
Copyright © 豆包嘿嘿~ 2012-∞ 冀ICP备17033181号 all right reserved,powered by Gitbook修订: 2012-12-31 12:00:00

results matching ""

    No results matching ""