美文网首页
CentOS7下源码安装MongoDB

CentOS7下源码安装MongoDB

作者: 程序员小华 | 来源:发表于2019-04-17 14:21 被阅读0次
    1. 安装环境

    系统:CentOS 7
    MongoDB版本:mongodb-linux-x86_64-4.0.9.tgz

    2. 首先从mongoDB官网下载mongodb安装包
    3. 通过ftp工具将安装包上传至CentOS系统中,解压并且重命名为mongodb
    [root@localhost local]# tar -zxvf mongodb-linux-x86_64-4.0.9.tgz 
    mongodb-linux-x86_64-4.0.9/README
    mongodb-linux-x86_64-4.0.9/THIRD-PARTY-NOTICES
    mongodb-linux-x86_64-4.0.9/MPL-2
    mongodb-linux-x86_64-4.0.9/LICENSE-Community.txt
    mongodb-linux-x86_64-4.0.9/bin/mongodump
    mongodb-linux-x86_64-4.0.9/bin/mongorestore
    mongodb-linux-x86_64-4.0.9/bin/mongoexport
    mongodb-linux-x86_64-4.0.9/bin/mongoimport
    mongodb-linux-x86_64-4.0.9/bin/mongostat
    mongodb-linux-x86_64-4.0.9/bin/mongotop
    mongodb-linux-x86_64-4.0.9/bin/bsondump
    mongodb-linux-x86_64-4.0.9/bin/mongofiles
    mongodb-linux-x86_64-4.0.9/bin/mongoreplay
    mongodb-linux-x86_64-4.0.9/bin/mongod
    mongodb-linux-x86_64-4.0.9/bin/mongos
    mongodb-linux-x86_64-4.0.9/bin/mongo
    mongodb-linux-x86_64-4.0.9/bin/install_compass
    [root@localhost local]# mv mongodb-linux-x86_64-4.0.9 mongodb
    [root@localhost local]# ll
    总用量 82980
    drwxr-xr-x. 2 root root        6 4月  11 2018 bin
    drwxr-xr-x. 2 root root        6 4月  11 2018 etc
    drwxr-xr-x. 2 root root        6 4月  11 2018 games
    drwxr-xr-x. 2 root root        6 4月  11 2018 include
    drwxr-xr-x. 2 root root        6 4月  11 2018 lib
    drwxr-xr-x. 2 root root        6 4月  11 2018 lib64
    drwxr-xr-x. 2 root root        6 4月  11 2018 libexec
    drwxr-xr-x. 3 root root      100 4月  17 13:42 mongodb
    -rw-r--r--. 1 root root 84969698 4月  17 13:38 mongodb-linux-x86_64-4.0.9.tgz
    drwxr-xr-x. 2 root root        6 4月  11 2018 sbin
    drwxr-xr-x. 5 root root       49 4月  16 17:14 share
    drwxr-xr-x. 2 root root        6 4月  11 2018 src
    [root@localhost local]# 
    
    4. 在配置文件 /etc/profile 配置环境变量,添加以下语句
    export MONGODB_HOME=/usr/local/mongodb
    export PATH=$MONGODB_HOME/bin:$PATH
    

    编辑 /etc/profile 配置文件使用以下命令

    [root@localhost local]# vim /etc/profile    # 编辑
    [root@localhost local]# source /etc/profile    # 使修改的配置生效
    
    5. 查看mongoDB的版本信息
    [root@localhost bin]# pwd
    /usr/local/mongodb/bin
    [root@localhost bin]# mongod -v
    2019-04-17T13:47:17.668+0800 D NETWORK  [main] fd limit hard:4096 soft:1024 max conn: 819
    2019-04-17T13:47:17.670+0800 I CONTROL  [initandlisten] MongoDB starting : pid=7596 port=27017 dbpath=/data/db 64-bit host=localhost.localdomain
    2019-04-17T13:47:17.670+0800 I CONTROL  [initandlisten] db version v4.0.9
    2019-04-17T13:47:17.670+0800 I CONTROL  [initandlisten] git version: fc525e2d9b0e4bceff5c2201457e564362909765
    2019-04-17T13:47:17.670+0800 I CONTROL  [initandlisten] allocator: tcmalloc
    2019-04-17T13:47:17.670+0800 I CONTROL  [initandlisten] modules: none
    2019-04-17T13:47:17.670+0800 I CONTROL  [initandlisten] build environment:
    2019-04-17T13:47:17.670+0800 I CONTROL  [initandlisten]     distarch: x86_64
    2019-04-17T13:47:17.670+0800 I CONTROL  [initandlisten]     target_arch: x86_64
    2019-04-17T13:47:17.670+0800 I CONTROL  [initandlisten] options: { systemLog: { verbosity: 1 } }
    2019-04-17T13:47:17.670+0800 D NETWORK  [initandlisten] fd limit hard:4096 soft:1024 max conn: 819
    2019-04-17T13:47:17.672+0800 D -        [initandlisten] User Assertion: NonExistentPath: Data directory /data/db not found. src/mongo/db/storage/storage_engine_init.cpp 243
    2019-04-17T13:47:17.672+0800 I STORAGE  [initandlisten] exception in initAndListen: NonExistentPath: Data directory /data/db not found., terminating
    2019-04-17T13:47:17.673+0800 D -        [initandlisten] User Assertion: NotMaster: not primary so can't step down src/mongo/db/db.cpp 893
    2019-04-17T13:47:17.673+0800 I NETWORK  [initandlisten] shutdown: going to close listening sockets...
    2019-04-17T13:47:17.673+0800 I NETWORK  [initandlisten] removing socket file: /tmp/mongodb-27017.sock
    2019-04-17T13:47:17.673+0800 I CONTROL  [initandlisten] now exiting
    2019-04-17T13:47:17.673+0800 I CONTROL  [initandlisten] shutting down with code:100
    [root@localhost bin]# 
    

    看到以上信息证明mongoDB安装成功,下面对mongoDB数据库进行一些常用配置

    6. 创建数据库文件夹以及日志文件夹
    [root@localhost mongodb]# pwd
    /usr/local/mongodb
    [root@localhost mongodb]# mkdir -p data
    [root@localhost mongodb]# mkdir -p log
    [root@localhost mongodb]# touch log/mongodb.log
    [root@localhost mongodb]# 
    
    7. 创建数据库配置文件mongodb.conf

    在 /usr/local/mongodb/bin 路径下创建数据库配置文件mongodb.conf,并添加以下内容

    # 数据库路径
    dbpath=/usr/local/mongodb/data
    # 日志文件路径
    logpath=/usr/local/mongodb/log/mongodb.log
    # 表示日志文件末尾追加日志
    logappend=true
    # 启用端口号
    port=27017
    # 是否在后台运行
    fork=true
    
    8. 通过配置文件启动mongoDB
    [root@localhost bin]# mongod -f ./mongodb.conf 
    about to fork child process, waiting until server is ready for connections.
    forked process: 7662
    child process started successfully, parent exiting
    [root@localhost bin]# 
    

    看到以上消息代表启动成功

    8. 新建数据库用户

    mongoDB默认情况下是没有启用验证的,但是在实际生产环境中,我们必须启用验证,所以一般在配置完mongoDB后,使用后台管理shell连接mongoDB新建用户,

    [root@localhost bin]# pwd
    /usr/local/mongodb/bin
    [root@localhost bin]# ./mongo
    MongoDB shell version v4.0.9
    connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
    Implicit session: session { "id" : UUID("d50df87b-5f07-46f3-8bde-8a041c5a13cc") }
    MongoDB server version: 4.0.9
    Server has startup warnings: 
    2019-04-17T14:05:00.584+0800 I CONTROL  [initandlisten] 
    2019-04-17T14:05:00.584+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
    2019-04-17T14:05:00.584+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
    2019-04-17T14:05:00.584+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
    2019-04-17T14:05:00.584+0800 I CONTROL  [initandlisten] 
    2019-04-17T14:05:00.584+0800 I CONTROL  [initandlisten] 
    2019-04-17T14:05:00.584+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
    2019-04-17T14:05:00.584+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
    2019-04-17T14:05:00.584+0800 I CONTROL  [initandlisten] 
    2019-04-17T14:05:00.584+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
    2019-04-17T14:05:00.584+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
    2019-04-17T14:05:00.584+0800 I CONTROL  [initandlisten] 
    > 
    

    在这里新建两个用户,一个是在admin数据库的管理用户huaAdmin,主要用于管理mongoDB,另外一个是业务逻辑数据库hua的用户,主要用于处理数据库的增删改查

    > use admin
    switched to db admin
    > db.createUser(
    ...   {
    ...     user: "huaAdmin",
    ...     pwd: "huaAdmin",
    ...     roles: [ { role: "userAdminAnyDatabase", db: "admin" },{role:"hostManager", db:"admin"} ]
    ...   }
    ... );
    Successfully added user: {
        "user" : "huaAdmin",
        "roles" : [
            {
                "role" : "userAdminAnyDatabase",
                "db" : "admin"
            },
            {
                "role" : "hostManager",
                "db" : "admin"
            }
        ]
    }
    > use hua
    switched to db hua
    > db.createUser(
    ... {
    ... user: "hua",
    ... pwd: "hua",
    ... roles: [{ role: "readWrite", db: "hua" },{ role: "dbAdmin", db: "hua" }]
    ... }
    ... );
    Successfully added user: {
        "user" : "hua",
        "roles" : [
            {
                "role" : "readWrite",
                "db" : "hua"
            },
            {
                "role" : "dbAdmin",
                "db" : "hua"
            }
        ]
    }
    > 
    

    新建完用户之后,关闭mongoDB服务

    > use admin
    switched to db admin
    > db.shutdownServer();    # 关闭mongoDB服务
    server should be down...
    2019-04-17T14:15:41.731+0800 I NETWORK  [js] trying reconnect to 127.0.0.1:27017 failed
    2019-04-17T14:15:41.732+0800 I NETWORK  [js] reconnect 127.0.0.1:27017 failed failed 
    > exit
    bye
    2019-04-17T14:15:43.360+0800 I NETWORK  [js] trying reconnect to 127.0.0.1:27017 failed
    2019-04-17T14:15:43.361+0800 I NETWORK  [js] reconnect 127.0.0.1:27017 failed failed 
    2019-04-17T14:15:43.361+0800 I QUERY    [js] Failed to end session { id: UUID("d50df87b-5f07-46f3-8bde-8a041c5a13cc") } due to SocketException: socket exception [CONNECT_ERROR] server [couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused]
    [root@localhost bin]# 
    
    9. 编辑配置文件mongodb.conf,在末尾添加 auth=true,开启验证

    再次启动mongoDB,然后使用Navicat连接(必须关闭CentOS防火墙)

    [root@localhost bin]# systemctl stop firewalld   # 关闭防火墙
    [root@localhost bin]# mongod -f ./mongodb.conf     # 启动mongoDB
    about to fork child process, waiting until server is ready for connections.
    forked process: 7748
    child process started successfully, parent exiting
    [root@localhost bin]# 
    

    至此,MongoDB安装完毕。

    相关文章

      网友评论

          本文标题:CentOS7下源码安装MongoDB

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