微軟 CEO 談科技公司壟斷:微軟站在歷史正確的一邊
阿新 • • 發佈:2021-07-15
目錄
linux安裝mongodb
啟動mongodb服務
mongodb整合到asp .net Core Web Api專案(原始碼可下載)
1.安裝依賴
yum install libcurl openssl
2.下載,解壓
wget -c https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.5.tgz tar -zxvf mongodb-linux-x86_64-4.0.5.tgz
3.移動mongodb
mkdir -p /usr/local/mongodb mv mongodb-linux-x86_64-4.0.5/* /usr/local/mongodb
4.加入環境變數
vim /etc/profile #下面程式碼加入檔案末尾 PATH=$PATH:/usr/local/mongodb/bin export PATH #立刻生效 source /etc/profile #檢視版本 [root@jmsite ~]# mongod --version db version v4.0.5 git version: 3739429dd92b92d1b0ab120911a23d50bf03c412 allocator: tcmalloc modules: none build environment: distarch: x86_64 target_arch: x86_64
5.建立資料目錄,日誌目錄,配置檔案目錄,程序檔案目錄
mkdir -p /usr/local/mongodb/data mkdir -p /usr/local/mongodb/log mkdir -p /usr/local/mongodb/conf mkdir -p /var/run/mongodb
6.建立配置檔案
vim /usr/local/mongodb/conf/mongodb.conf
寫入以下配置
#監聽埠 port = 27017 #資料庫存檔案存放目錄 dbpath = /usr/local/mongodb/data #日誌檔案存放路徑 logpath = /usr/local/mongodb/log/mongodb.log #程序檔案存放路徑 pidfilepath= /var/run/mongodb/mongodb.pid #使用追加的方式寫日誌 logappend = true #以守護程序的方式執行,建立伺服器程序 fork = true #最大同時連線數 maxConns = 100 #每次寫入會記錄一條操作日誌 journal = true #不繫結ip,這樣就可外部訪問了 bind_ip = 0.0.0.0 #校驗許可權 auth = true
7.建立一個不能登入的mongodb使用者並設定許可權
useradd mongodb -M -s /sbin/nologin chown -R mongodb.mongodb /usr/local/mongodb chown -R mongodb.mongodb /var/run/mongodb
8.建立啟動指令碼
vim /etc/init.d/mongod
寫入以下配置
#!/bin/sh # # mongodb init file for starting up the MongoDB server # # chkconfig: - 90 10 # description: Starts and stops the MongDB daemon that handles all \ # database requests. # Source function library. . /etc/rc.d/init.d/functions exec="/usr/local/mongodb/bin/mongod" prog="mongod" logfile="/usr/local/mongodb/log/mongodb.log" [ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog OPTIONS="--quiet -f /usr/local/mongodb/conf/mongodb.conf" pidfile=${PIDFILE-/var/run/mongodb/mongodb.pid} options="$MONGODB_OPTIONS $OPTIONS" lockfile="/var/lock/subsys/mongod" # Nicer version of killproc that does not kill mongodb when it takes # a long time to shut down and does not hang for a long time when mongo # shuts down quickly killproc_nice() { local RC base pid pid_file= delay i RC=0; delay=3 # Test syntax. if [ "$#" -eq 0 ]; then echo $"Usage: killproc [-p pidfile] [ -d delay] {program} [-signal]" return 1 fi if [ "$1" = "-p" ]; then pid_file=$2 shift 2 fi if [ "$1" = "-d" ]; then delay=$2 shift 2 fi # Save basename. base=${1##*/} # Find pid. __pids_var_run "$1" "$pid_file" RC=$? if [ -z "$pid" ]; then if [ -z "$pid_file" ]; then pid="$(__pids_pidof "$1")" else [ "$RC" = "4" ] && { failure $"$base shutdown" ; return $RC ;} fi fi # Kill it. if [ -n "$pid" ] ; then [ "$BOOTUP" = "verbose" -a -z "${LSB:-}" ] && echo -n "$base " if checkpid $pid 2>&1; then # TERM first, then KILL if not dead kill -TERM $pid >/dev/null 2>&1 usleep 100000 # Check every one second if the program is stopped. # Do so for a maximum of $delay seconds for ((i = 0 ; i < $delay; i++)) do if checkpid $pid; then sleep 1 else break fi done # If the program is not stopped, kill it if checkpid $pid ; then kill -KILL $pid >/dev/null 2>&1 usleep 100000 fi fi checkpid $pid RC=$? [ "$RC" -eq 0 ] && failure $"$base shutdown" || success $"$base shutdown" RC=$((! $RC)) else failure $"$base shutdown" RC=0 fi # Remove pid file if any. rm -f "${pid_file:-/var/run/$base.pid}" return $RC } start() { [ -x $exec ] || exit 5 echo -n $"Starting $prog: " daemon --pidfile=${pidfile} --user mongodb "$exec $options run >> $logfile 2>&1" retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc_nice -p ${pidfile} -d 300 $prog retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { stop start } reload() { restart } force_reload() { restart } rh_status() { # run checks to determine if the service is running or use generic status status -p ${pidfile} $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 restart ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}" exit 2 esac exit $?
設定許可權
chmod 755 /etc/init.d/mongod
加入服務
chkconfig --add mongod
開機啟動
chkconfig mongod on