centos7 编译安装 mongodb


wget https://fastdl.mongodb.org/src/mongodb-src-r5.0.12.tar.gz
https://www.mongodb.com/try/download/community

编译

编译过程中的排错,就是保证安装脚本环境检测通过。

[root@atom mongodb-src-r5.0.12]#python3 buildscripts/scons.py --link-model=static --opt=on all
No module named 'requirements'
Try running:
    /bin/python3 -m pip install requirements_parser
[root@atom mongodb-src-r5.0.12]#pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple requirements_parser
[root@atom mongodb-src-r5.0.12]#python3 buildscripts/scons.py --link-model=static --opt=on all
Checking required python packages...
Requirements list:
    Cheetah3<=3.2.6.post1
    PyYAML<=6.0.0,>=3.0.0
    oauthlib==3.1.1
    pkce==1.0.3
    psutil<=5.8.0
    pymongo<4.0,>=3.9
    pypiwin32==223; sys_platform == "win32" and python_version > "3"
    pywin32==225; sys_platform == "win32" and python_version > "3"
    regex<=2021.11.10
    requests-oauthlib==1.3.0
    requests<=2.26.0,>=2.0.0
    requirements_parser<=0.3.1
    setuptools
(requirements-parser 0.5.0 (/usr/local/lib/python3.6/site-packages), Requirement.parse('requirements_parser<=0.3.1'))
Try running:
    /bin/python3 -m pip install -r etc/pip/compile-requirements.txt
[root@atom mongodb-src-r5.0.12]#pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -r etc/pip/compile-requirements.txt
...
#报错
    ----------------------------------------
Command "/usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-blnzck8d/psutil/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-7sgto497-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-blnzck8d/psutil/
[root@atom mongodb-src-r5.0.12]# yum install gcc python3-devel
#再运行requirements.txt 则 Successfully installed
#再编译 
[root@atom mongodb-src-r5.0.12]#python3 buildscripts/scons.py --link-model=static --opt=on all
sh: g++: command not found
[root@atom mongodb-src-r5.0.12]#yum install gcc-c++
gcc: error: unrecognized command line option '-mprefer-vector-width=128'
#需要编译安装gcc,版本要求8.0及以上,我编译的版本要求8.2

编译安装 gcc 8.2

gcc: error: unrecognized command line option '-mprefer-vector-width=128'         #当前的gcc版本不够,编译安装 gcc 8.2

https://www.jianshu.com/p/444169a3721a?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

[root@atom ~]#wget https://src.fedoraproject.org/repo/pkgs/gcc/isl-0.18.tar.bz2/sha512/85d0b40f4dbf14cb99d17aa07048cdcab2dc3eb527d2fbb1e84c41b2de5f351025370e57448b63b2b8a8cf8a0843a089c3263f9baee1542d5c2e1cb37ed39d94/isl-0.18.tar.bz2
[root@atom ~]#tar -xJvf gmp-6.1.0.tar.xz

https://src.fedoraproject.org/repo/pkgs/gcc/isl-0.18.tar.bz2/

ln -sf /usr/local/gcc-8.2.0/bin/gcc /usr/bin/gcc
ln -sf /usr/local/gcc-8.2.0/bin/g++  /usr/bin/g++ 
ln -sf /usr/local/gcc-8.2.0/bin/gcc-ar /usr/bin/gcc-ar
ln -sf /usr/local/gcc-8.2.0/bin/gcc-nm  /usr/bin/gcc-nm 
ll /usr/bin/gcc*
g++ -v

Couldn’t find OpenSSL crypto.h header and library


#编译安装openssl
wget https://www.openssl.org/source/old/3.0/openssl-3.0.0.tar.gz
#编译安装后再运行
[root@atom mongodb-src-r5.0.12]#python3 buildscripts/scons.py --link-model=static --opt=on all --ssl CPPPATH=/usr/local/openssl/include LIBPATH=/usr/local/openssl/lib64

https://blog.csdn.net/www_dong/article/details/124482288


Cannot find system library ‘lzma’ required for use with libunwind

#报错
/bin/ld.gold: error: cannot find -llzma
collect2: error: ld returned 1 exit status
scons: Configure: no
[root@atom lib]#/bin/ld.gold  -llzma --verbose
/bin/ld.gold: Attempt to open //lib/liblzma.so failed
/bin/ld.gold: Attempt to open //lib/liblzma.a failed
/bin/ld.gold: Attempt to open //usr/lib/liblzma.so failed
/bin/ld.gold: Attempt to open //usr/lib/liblzma.a failed
/bin/ld.gold: error: cannot find -llzma
/bin/ld.gold: Opened new descriptor 3 for "a.out"
[root@atom lib]#find /usr/ -name liblzma.so.0
/usr/lib64/liblzma.so.0 
[root@atom lib]#ln -s /usr/lib64/liblzma.so.
liblzma.so.0      liblzma.so.0.0.0  liblzma.so.5      liblzma.so.5.2.2  
[root@atom lib]#ln -s /usr/lib64/liblzma.so.
liblzma.so.0      liblzma.so.0.0.0  liblzma.so.5      liblzma.so.5.2.2  
[root@atom lib]#ln -s /usr/lib64/liblzma.so.0 /usr/lib/liblzma.so
[root@atom lib]#/bin/ld.gold  -llzma --verbose
/bin/ld.gold: Opened new descriptor 3 for "//lib/liblzma.so"
/bin/ld.gold: Attempt to open //lib/liblzma.so succeeded
/bin/ld.gold: Unlocking file "//lib/liblzma.so"
/bin/ld.gold: Released descriptor 3 for "//lib/liblzma.so"
/bin/ld.gold: Opened new descriptor 4 for "a.out"

Could not find <curl/curl.h> and curl lib

 fatal error: curl/curl.h: No such file or directory
 yum install -y libcurl-devel      #楼下的老哥指出来了,centos7 也因该是这个

Do not know how to make File target `all’

https://github.com/mongodb/mongo/blob/master/docs/building.md

scons: done reading SConscript files.
scons: Building targets ...
scons: *** Do not know how to make File target `all' (/root/mongodb-src-r5.0.12/all).  Stop.
scons: building terminated because of errors.
all failed: Do not know how to make File target `all' (/root/mongodb-src-r5.0.12/all).
[root@atom mongodb-src-r5.0.12]#python3 buildscripts/scons.py --link-model=static --opt=on  --ssl CPPPATH=/usr/local/openssl/include LIBPATH=/usr/local/openssl/lib64 --disable-warnings-as-errors DESTDIR=/tmp/mongo PREFIX=/usr/local/mongodb install-all 

collect2: fatal error: ld terminated with signal 9 [Killed]

scons: Building targets ...
Linking build/59f4f0dd/mongo/db/mongod
collect2: fatal error: ld terminated with signal 9 [Killed]
compilation terminated.
scons: *** [build/59f4f0dd/mongo/db/mongod] Error 1
scons: building terminated because of errors.
build/59f4f0dd/mongo/db/mongod failed: Error 1

由于编译耗资源,运行内存不够,这里我用的是虚拟机,则重新配置增加内存。

No space left on device

[root@atom mongodb-src-r5.0.12]#python3 buildscripts/scons.py --link-model=static --opt=on  \
--ssl CPPPATH=/usr/local/openssl/include LIBPATH=/usr/local/openssl/lib64 \
--disable-warnings-as-errors DESTDIR=/usr/local PREFIX=/mongodb install-devcore
[root@atom ~]#df -h
Filesystem                   Size  Used Avail Use% Mounted on
devtmpfs                     2.5G     0  2.5G   0% /dev
tmpfs                        2.5G  4.0K  2.5G   1% /dev/shm
tmpfs                        2.5G   12M  2.5G   1% /run
tmpfs                        2.5G     0  2.5G   0% /sys/fs/cgroup
/dev/mapper/centos_2ll-root   37G   37G   20K 100% /
/dev/sda1                   1014M  267M  748M  27% /boot
tmpfs                        506M     0  506M   0% /run/user/1000
/bin/ld.gold: fatal error: build/opt/mongo/db/concurrency/db_concurrency_test: No space left on device
collect2: error: ld returned 1 exit status
scons: *** [build/opt/mongo/db/concurrency/db_concurrency_test] Error 1
scons: building terminated because of errors.
build/opt/mongo/db/concurrency/db_concurrency_test failed: Error 1

https://blog.csdn.net/carefree2005/article/details/111315592

在这里插入图片描述

编译完成

.....
Skipping ranlib for thin archive build/opt/mongo/db/exec/libstagedebug_cmd.a
Linking build/opt/mongo/db/mongod
Install file: "build/opt/mongo/db/mongod" as "/usr/local/mongodb/bin/mongod"
scons: done building targets.
[root@atom mongodb-src-r5.0.12]#

去除程序调试信息

[root@atom bin]#ls -lah
total 3.4G
drwxr-xr-x. 2 root root   65 Oct 22 07:54 .
drwxr-xr-x. 3 root root  100 Oct 21 21:53 ..
-rwxr-xr-x. 1 root root 1.1G Oct 21 06:30 mongo
-rwxr-xr-x. 1 root root 104M Oct 22 07:54 mongod
-rwxr-xr-x. 1 root root 2.2G Oct 21 09:29 mongos
-rwxr--r--. 1 root root 1.8K Oct 21 21:53 resmoke.py
[root@atom bin]#objcopy --strip-debug ./mongo
[root@atom bin]#objcopy --strip-debug ./mongos
[root@atom bin]#ls -lah
total 230M
drwxr-xr-x. 2 root root   65 Oct 22 08:08 .
drwxr-xr-x. 3 root root  100 Oct 21 21:53 ..
-rwxr-xr-x. 1 root root  55M Oct 22 08:01 mongo
-rwxr-xr-x. 1 root root 104M Oct 22 07:54 mongod
-rwxr-xr-x. 1 root root  72M Oct 22 08:08 mongos
-rwxr--r--. 1 root root 1.8K Oct 21 21:53 resmoke.py
[root@atom bin]#

运行

解决mongod服务启动报错问题

[root@atom home]#mkdir -p /home/mongodb/data
[root@atom home]#mkdir -p /home/mongodb/log
[root@atom home]#vim /etc/profile
[root@localhost ~]#source  /etc/profile

#set for mongodb
export MONGODB_HOME=/usr/local/mongodb
export PATH=$MONGODB_HOME/bin:$PATH


[root@atom bin]#mongod
./mongod: error while loading shared libraries: libcrypto.so.3: cannot open shared object file: No such file or directory
[root@atom bin]#ln -s /usr/local/openssl/lib64/libcrypto.so.3  /lib64/libcrypto.so.3

[root@atom bin]#mongod
./mongod: error while loading shared libraries: libssl.so.3: cannot open shared object file: No such file or directory
[root@atom bin]#ln -s /usr/local/openssl/lib64/libssl.so.3 /lib64/libssl.so.3
[root@atom bin]#./mongod
./mongod: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by ./mongod)
./mongod: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.22' not found (required by ./mongod)
./mongod: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by ./mongod)
./mongod: /lib64/libstdc++.so.6: version `CXXABI_1.3.11' not found (required by ./mongod)
./mongod: /lib64/libstdc++.so.6: version `CXXABI_1.3.8' not found (required by ./mongod)



[root@atom bin]#cd /lib64/
[root@atom lib64]#ls -lah libstdc*
lrwxrwxrwx. 1 root root   19 Oct 23 01:28 libstdc++.so.6 -> libstdc++.so.6.0.19
-rwxr-xr-x. 1 root root 973K Sep 30  2020 libstdc++.so.6.0.19
[root@atom lib64]#strings libstdc++.so.6.0.19 | grep GLIB
GLIBCXX_3.4
GLIBCXX_3.4.1
GLIBCXX_3.4.2
GLIBCXX_3.4.3
GLIBCXX_3.4.4
GLIBCXX_3.4.5
GLIBCXX_3.4.6
GLIBCXX_3.4.7
GLIBCXX_3.4.8
GLIBCXX_3.4.9
GLIBCXX_3.4.10
GLIBCXX_3.4.11
GLIBCXX_3.4.12
GLIBCXX_3.4.13
GLIBCXX_3.4.14
GLIBCXX_3.4.15
GLIBCXX_3.4.16
GLIBCXX_3.4.17
GLIBCXX_3.4.18
GLIBCXX_3.4.19
GLIBC_2.3
GLIBC_2.2.5
GLIBC_2.14
GLIBC_2.4
GLIBC_2.3.2
GLIBCXX_DEBUG_MESSAGE_LENGTH
[root@atom lib64]#rm -rf libstdc++.so.6
[root@atom lib64]#ln -s /usr/local/gcc-8.2.0/lib64/libstdc++.so.6.0.25 ./libstdc++.so.6

运行mongod服务后(可以加个–fork,放后台运行,不过还得再ps找再kill,后面添加脚本来处理),发现可以运行mongo shell,初步编译及运行完成。
在这里插入图片描述

设置mongodb服务启动脚本

vim /etc/init.d/mongodb
chmod +x /etc/init.d/mongodb
[root@atom log]#service mongodb  start
Reloading systemd:                                         [  OK  ]
Starting mongodb (via systemctl):                          [  OK  ]
[root@atom log]#service mongodb  stop
Stopping mongodb (via systemctl):                          [  OK  ]
[root@atom log]#service mongodb  start
Starting mongodb (via systemctl):                          [  OK  ]
[root@atom log]#netstat -an | grep 27017
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN     
unix  2      [ ACC ]     STREAM     LISTENING     33824    /tmp/mongodb-27017.sock
[root@atom log]#

#!/bin/sh
#
#mongod - Startup script for mongod
#
# chkconfig: - 85 15
# description: Mongodb database.
# processname: mongod
# Source function library
 
. /etc/rc.d/init.d/functions
# things from mongod.conf get there by mongod reading it
# OPTIONS
OPTIONS=" -f /usr/local/mongodb/mongodb.conf &"						#读取配置,后台运行
#mongod
mongod="/usr/local/mongodb/bin/mongod"
lockfile=/var/lock/subsys/mongod
start()
{
  echo -n $"Starting mongod: "
  daemon $mongod $OPTIONS
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && touch $lockfile
}
 
stop()
{
  echo -n $"Stopping mongod: "
  killproc $mongod -QUIT
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && rm -f $lockfile
}
 
restart () {
        stop
        start
}
ulimit -n 12000
RETVAL=0
 
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart|reload|force-reload)
    restart
    ;;
  condrestart)
    [ -f $lockfile ] && restart || :
    ;;
  status)
    status $mongod
    RETVAL=$?
    ;;
  *)
    echo "Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
    RETVAL=1
esac
exit $RETVAL
#改密码记录
db.updateUser("admin", {pwd: "123456" })

db.changeUserPassword("admin", "123456")
[root@atom log]#cat /usr/local/mongodb/mongodb.conf 
port=27017 #端口
dbpath=/home/mongodb/data #数据库存文件存放目录
logpath=/home/mongodb/log/mongodb.log #日志文件存放路径
logappend=true #使用追加的方式写日志
fork=true #以守护进程的方式运行,创建服务器进程
maxConns=100 #最大同时连接数
auth=true #启用验证
journal=true #每次写入会记录一条操作日志(通过journal可以重新构造出写入的数据)。
#即使宕机,启动时wiredtiger会先将数据恢复到最近一次的checkpoint点,然后重放后续的journal日志来恢复。
storageEngine=wiredTiger  #存储引擎有mmapv1、wiretiger、mongorocks
bind_ip = 0.0.0.0  #这样就可外部访问了,例如从win10中去连虚拟机中的MongoDB
[root@atom log]#

首次进入配置

[root@atom log]#mongo
MongoDB shell version v5.0.12
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("9c7aeff7-2f09-44be-ba65-2d8df8fc28da") }
MongoDB server version: 5.0.12
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
> use admin
switched to db admin
> show dbs
> db.createUser(         // 创建管理员
...    {
...      user:"admin",
...      pwd:"123456",
...      roles:[{role:"root", db:"admin"}]
...     }
...  )
Successfully added user: {
        "user" : "admin",
        "roles" : [
                {
                        "role" : "root",
                        "db" : "admin"
                }
        ]
}
> exit
bye



[root@atom log]#mongo 127.0.0.1:27017/admin -u admin -p 123456  #密码设置成功进入以admin身份进入admin数据库

没有在启动时验证密码,则需要在mongo里进行auth
db.auth('admin', '123456')
> use tom-test;      // 创建tom-test数据库,进行插入使创建
switched to db tom-test
> db.users.insert({ name: 'hi' })
WriteResult({ "nInserted" : 1 })
> db.createUser({        // 创建tontom管理员用户
...   user: "tontom",
...   pwd: "123456",
...   roles: [
...     {role: "userAdmin", db: "tom-test"}
...   ]
... })
Successfully added user: {
        "user" : "tontom",
        "roles" : [
                {
                        "role" : "userAdmin",
                        "db" : "tom-test"
                }
        ]
}
> // 创建tonixtom用户
> db.createUser({      
...   user: "tonixtom",
...   pwd: "123456",
...   roles: [
...     {role: "readWrite", db: "tom-test"}
...   ]
... })
Successfully added user: {
        "user" : "tonixtom",
        "roles" : [
                {
                        "role" : "readWrite",
                        "db" : "tom-test"
                }
        ]
}
> 

[root@atom log]#mongo -u tontom -p 123456 tom-test --host 127.0.0.1
MongoDB shell version v5.0.12
connecting to: mongodb://127.0.0.1:27017/tom-test?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("5b0835b9-7f0c-4067-aaf0-0f9dbb30158f") }
MongoDB server version: 5.0.12
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
> exit
bye

到这里数据库本地配置及使用则完成



[root@atom log]#mongo -u tontom -p 123456 --host 192.168.31.177  tom-test
MongoDB shell version v5.0.12
connecting to: mongodb://192.168.31.177:27017/tom-test?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("e2d34f35-ba1b-4159-b9b7-cc77ac3f99fd") }
MongoDB server version: 5.0.12
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
> exit
bye

mongod远程配置

使用navicat或者MongoDB Compass做可以可视化远程。
先下载安装compass https://www.mongodb.com/try/download/shell
给它关掉
在这里插入图片描述

配置起来

在这里插入图片描述

可以看到连接不上,发现是firewall的问题
在这里插入图片描述

systemctl stop firewalld.service
#后者直接放行27017端口
firewall-cmd --add-port=27017/tcp --permanent
firewall-cmd --reload

可以看到没有问题,可以远程连接数据库服务器。

在这里插入图片描述

idea连接

DBMS: Mongo DB (ver. 5.0.12)
Case sensitivity: plain=mixed, delimited=mixed
Driver: MongoDB JDBC Driver (ver. 1.15, JDBC4.2)
Ping: 352 ms     #可以

参考
https://blog.csdn.net/carefree2005/article/details/111315592

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值