程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> Mysql數據庫之索引優化

Mysql數據庫之索引優化

編輯:MySQL綜合教程

Mysql數據庫之索引優化。本站提示廣大學習愛好者:(Mysql數據庫之索引優化)文章只能為提供參考,不一定能成為您想要的結果。以下是Mysql數據庫之索引優化正文


MySQL憑仗著精彩的機能、昂貴的本錢、豐碩的資本,曾經成為絕年夜多半互聯網公司的首選關系型數據庫。固然機能精彩,但所謂“好馬配好鞍”,若何可以或許更好的應用它,曾經成為開辟工程師的?課,我們常常會從職位描寫上看到諸如“精曉MySQL”、“SQL語句優化”、“懂得數據庫道理”等請求。我們曉得普通的運用體系,讀寫比例在10:1閣下,並且拔出操作和普通的更新操作很少湧現機能成績,碰到最多的,也是最輕易出成績的,照樣一些龐雜的查詢操作,所以查詢語句的優化明顯是重中之重。

成績:cpu負載太高,到達36。


景象:經由過程mysqladmin -uroot -p processlist 檢查到年夜量以下信息:

Sending data select * from `rep_corp_vehicle_online_count` where corp_id = 48 and vehicle_id = 10017543

依據以上的能夠是表rep_corp_vehicle_online_count的成績 做出以下測試:

檢查表構造:

mysql> desc rep_corp_vehicle_online_count;
+-------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| corp_id | int(11) | NO | | NULL | |
| vehicle_id | int(11) | NO | | NULL | |
| online_day | varchar(20) | NO | | NULL | |
| loc_total | int(11) | NO | | NULL | |
| create_time | datetime | NO | | NULL | |
| update_time | datetime | NO | | NULL | |
+-------------+-------------+------+-----+---------+----------------+
7 rows in set (0.00 sec) 

檢查索引,只要主鍵索引:

mysql> show index from rep_corp_vehicle_online_count;
+-------------------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------------------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| rep_corp_vehicle_online_count | 0 | PRIMARY | 1 | id | A | 1247259 | NULL | NULL | | BTREE | | |
+-------------------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec) 

代碼履行情形:

mysql>explain select * from rep_corp_vehicle_online_count where corp_id = 79 and vehicle_id = 10016911 and online_day = '2016-03-29'\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: rep_corp_vehicle_online_count
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 1248495
Extra: Using where
1 row in set (0.00 sec) 

表數據剖析情形,反復數據許多:

mysql> select count(distinct corp_id) from rep_corp_vehicle_online_count;
+-------------------------+
| count(distinct corp_id) |
+-------------------------+
| 18 |
+-------------------------+
1 row in set (0.63 sec)
mysql> select count(corp_id) from rep_corp_vehicle_online_count; 
+----------------+
| count(corp_id) |
+----------------+
| 1239573 |
+----------------+
1 row in set (0.00 sec)
mysql> select count(distinct vehicle_id) from rep_corp_vehicle_online_count; 
+----------------------------+
| count(distinct vehicle_id) |
+----------------------------+
| 2580 |
+----------------------------+
1 row in set (1.03 sec)
mysql>explain select count(vehicle_id) from rep_corp_vehicle_online_count; 
+-------------------+
| count(vehicle_id) |
+-------------------+
| 1239911 |
+-------------------+
1 row in set (0.00 sec) 

最初處置,創立索引:

mysql> create index r_c_v on rep_corp_vehicle_online_count(corp_id,vehicle_id); 
Query OK, 1487993 rows affected (6.09 sec)
Records: 1487993 Duplicates: 0 Warnings: 0
mysql> show index from rep_corp_vehicle_online_count;
+-------------------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------------------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| rep_corp_vehicle_online_count | 0 | PRIMARY | 1 | id | A | 1490176 | NULL | NULL | | BTREE | | |
| rep_corp_vehicle_online_count | 1 | r_c_v | 1 | corp_id | A | 18 | NULL | NULL | | BTREE | | |
| rep_corp_vehicle_online_count | 1 | r_c_v | 2 | vehicle_id | A | 2596 | NULL | NULL | | BTREE | | |
+-------------------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)

添加索引事後負載下降到了1.73:

以上內容是小編給年夜家引見的Mysql數據庫之索引優化 ,願望對年夜家進修有所贊助!

mongodb-linux-x86_64-rhel62-3.2.5-20-g07e21d8 [root@yq-mapp-otadb248 local]# chown -R mongodb:mongodb /usr/local/mongodb [root@yq-mapp-otadb248 local]# [root@yq-mapp-otadb248 local]# ll total 52 drwxr-xr-x. 2 root root 4096 Apr 24 2014 bin drwxr-xr-x. 2 root root 4096 Sep 23 2011 etc drwxr-xr-x. 2 root root 4096 Sep 23 2011 games drwxr-xr-x. 2 root root 4096 Sep 23 2011 include drwxr-xr-x. 2 root root 4096 Sep 23 2011 lib drwxr-xr-x. 3 root root 4096 Apr 25 2014 lib64 drwxr-xr-x. 2 root root 4096 Sep 23 2011 libexec lrwxrwxrwx 1 root root 25 Mar 6 2015 logstash -> /usr/local/logstash-1.4.2 drwxrwxr-x 8 logstash logstash 4096 Jun 24 2014 logstash-1.4.2 lrwxrwxrwx 1 mongodb mongodb 45 Oct 19 11:59 mongodb -> mongodb-linux-x86_64-rhel62-3.2.5-20-g07e21d8 drwxr-xr-x 3 mongodb mongodb 4096 Oct 19 11:54 mongodb-linux-x86_64-rhel62-3.2.5-20-g07e21d8 lrwxrwxrwx 1 root root 39 Apr 23 2014 mysql -> /usr/local/mysql-5.5.19-linux2.6-x86_64 drwxr-xr-x 12 root mysql 4096 Apr 23 2014 mysql-5.5.19-linux2.6-x86_64 drwxr-xr-x. 2 root root 4096 Sep 23 2011 sbin drwxr-xr-x. 6 root root 4096 Apr 25 2014 share drwxr-xr-x. 2 root root 4096 Oct 19 11:54 src [root@yq-mapp-otadb248 local]#

4. 創立mongodb實例所需的目次和設置裝備擺設文件

創立mongodb實例所需目次:

mkdir -p /data/mongo_27117/{db,log,tmp}

創立mongodb實例設置裝備擺設文件所需目次和文件:

mkdir -p /etc/mongodb
touch /etc/mongodb/mongo_27117.conf

依據須要設置裝備擺設mongodb的啟動參數,我的啟動參數設置裝備擺設內容以下:

vim /etc/mongodb/mongo_27117.conf

dbpath=/data/mongo_27117/db 
logpath=/data/mongo_27117/log/mongo_27117.log 
pidfilepath = /data/mongo_27117/tmp/mongo_27117.pid 
storageEngine = wiredTiger 
wiredTigerCacheSizeGB = 2 
syncdelay = 30 
wiredTigerCollectionBlockCompressor = zlib 
port=27117 
auth = true 
directoryperdb = true 
oplogSize=2048 
logappend=true 
fork=true 
#rest=true 
journal = true 
journalCommitInterval = 50 
slowms = 200

修正目次、文件的權限和屬組:

chown -R mongodb:mongodb /data/mongo_27117/
chown -R mongodb:mongodb /etc/mongodb

確認目次和設置裝備擺設文件都曾經預備終了:

ls -l /data/mongo_27117/
ls -l /etc/mongodb
cat /etc/mongodb/mongo_27117.conf

該步調現實操作進程以下:

[root@yq-mapp-otadb248 local]# 
[root@yq-mapp-otadb248 local]# mkdir -p /data/mongo_27117/{db,log,tmp} 
[root@yq-mapp-otadb248 local]# mkdir -p /etc/mongodb 
[root@yq-mapp-otadb248 local]# 
[root@yq-mapp-otadb248 local]# 
[root@yq-mapp-otadb248 local]# vim /etc/mongodb/mongo_27117.conf 
dbpath=/data/mongo_27117/db 
logpath=/data/mongo_27117/log/mongo_27117.log 
pidfilepath = /data/mongo_27117/tmp/mongo_27117.pid 
storageEngine = wiredTiger 
wiredTigerCacheSizeGB = 2 
syncdelay = 30 
wiredTigerCollectionBlockCompressor = zlib 
port=27117 
auth = true 
directoryperdb = true 
oplogSize=2048 
logappend=true 
fork=true 
#rest=true 
journal = true 
journalCommitInterval = 50 
slowms = 200 
~ 
[root@yq-mapp-otadb248 local]# 
[root@yq-mapp-otadb248 local]# chown -R mongodb:mongodb /data/mongo_27117/ 
[root@yq-mapp-otadb248 local]# chown -R mongodb:mongodb /etc/mongodb 
[root@yq-mapp-otadb248 local]# ls -l /data/mongo_27117/ 
total 12 
drwxr-xr-x 2 mongodb mongodb 4096 Oct 19 12:02 db 
drwxr-xr-x 2 mongodb mongodb 4096 Oct 19 12:02 log 
drwxr-xr-x 2 mongodb mongodb 4096 Oct 19 12:02 tmp 
[root@yq-mapp-otadb248 local]# 
[root@yq-mapp-otadb248 local]# 
[root@yq-mapp-otadb248 local]# ls -l /etc/mongodb 
total 4 
-rw-r--r-- 1 mongodb mongodb 392 Oct 19 12:05 mongo_27117.conf 
[root@yq-mapp-otadb248 local]# 
[root@yq-mapp-otadb248 local]# cat /etc/mongodb/mongo_27117.conf 
dbpath=/data/mongo_27117/db 
logpath=/data/mongo_27117/log/mongo_27117.log 
pidfilepath = /data/mongo_27117/tmp/mongo_27117.pid 
storageEngine = wiredTiger 
wiredTigerCacheSizeGB = 2 
syncdelay = 30 
wiredTigerCollectionBlockCompressor = zlib 
port=27117 
auth = true 
directoryperdb = true 
oplogSize=2048 
logappend=true 
fork=true 
#rest=true 
journal = true 
journalCommitInterval = 50 
slowms = 200

5. 啟動mongodb實例,修正治理員暗碼

應用上面的敕令啟動mongodb辦事:

/usr/local/mongodb/bin/mongod --config /etc/mongodb/mongo_27117.conf

確認暗碼,mongodb的治理員暗碼,可以依據各自的規矩設置:

echo $MONGODB_ROOT_PASS
mongodb_020248_Pass

修正治理員暗碼,留意 mongodb 3.2要對admin授與三個腳色,這點與之前的版本分歧:

/usr/local/mongodb/bin/mongo --port=27117
db.createUser({user:'useradmin',pwd:'mongodb_020248_Pass',roles:[ { "role" : "clusterAdmin", "db" : "admin" }, { "role" : "userAdminAnyDatabase", "db" : "admin" }, { "role" : "dbAdminAnyDatabase", "db" : "admin" } ] })
db.auth("useradmin","mongodb_020248_Pass")
db.system.users.find();

生成暗碼後,應用新用戶和暗碼上岸mongo,確認狀況:

/usr/local/mongodb/bin/mongo --port=27117 -u useradmin -p mongodb_020248_Pass --authenticationDatabase admin

該步調操作進程以下;

[root@yq-mapp-otadb248 local]# 
[root@yq-mapp-otadb248 local]# ps -ef|grep mongo 
root 32295 30115 0 12:12 pts/0 00:00:00 grep mongo 
[root@yq-mapp-otadb248 local]# 
[root@yq-mapp-otadb248 local]# /usr/local/mongodb/bin/mongod --config /etc/mongodb/mongo_27117.conf 
about to fork child process, waiting until server is ready for connections. 
forked process: 32321 
child process started successfully, parent exiting 
[root@yq-mapp-otadb248 local]# 
[root@yq-mapp-otadb248 local]# ps -ef|grep mongo 
root 32321 1 1 12:12 ? 00:00:00 /usr/local/mongodb/bin/mongod --config /etc/mongodb/mongo_27117.conf 
root 32359 30115 0 12:13 pts/0 00:00:00 grep mongo 
[root@yq-mapp-otadb248 local]# 
[root@yq-mapp-otadb248 local]# 
[root@yq-mapp-otadb248 local]# /usr/local/mongodb/bin/mongo --port=27117 
MongoDB shell version: 3.2.5-20-g07e21d8 
connecting to: 127.0.0.1:27117/test 
Welcome to the MongoDB shell. 
For interactive help, type "help". 
For more comprehensive documentation, see 
http://docs.mongodb.org/ 
Questions? Try the support group 
http://groups.谷歌.com/group/mongodb-user 
> 
> use admin; 
switched to db admin 
> db.system.users.find(); 
Error: error: { 
"ok" : 0, 
"errmsg" : "not authorized on admin to execute command { find: \"system.users\", filter: {} }", 
"code" : 13 
} 
> 
> db.createUser({user:'useradmin',pwd:'mongodb_020248_@JJMatch',roles:[ { "role" : "clusterAdmin", "db" : "admin" }, { "role" : "userAdminAnyDatabase", "db" : "admin" }, { "role" : "dbAdminAnyDatabase", "db" : "admin" } ] }) 
Successfully added user: { 
"user" : "useradmin", 
"roles" : [ 
{ 
"role" : "clusterAdmin", 
"db" : "admin" 
}, 
{ 
"role" : "userAdminAnyDatabase", 
"db" : "admin" 
}, 
{ 
"role" : "dbAdminAnyDatabase", 
"db" : "admin" 
} 
] 
} 
> 
> db.system.users.find(); 
Error: error: { 
"ok" : 0, 
"errmsg" : "not authorized on admin to execute command { find: \"system.users\", filter: {} }", 
"code" : 13 
} 
> 
> db.auth("useradmin","mongodb_020248_@JJMatch") 
1 
> 
> db.system.users.find(); 
{ "_id" : "admin.useradmin", "user" : "useradmin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "6hLx/d97hS+yfoN47QTmXQ==", "storedKey" : "B0PqwVs3GFKIHQyyQ6mBp1MA370=", "serverKey" : "xK53AKKAvFCdn5rsEtij5QB9RtU=" } }, "roles" : [ { "role" : "clusterAdmin", "db" : "admin" }, { "role" : "userAdminAnyDatabase", "db" : "admin" }, { "role" : "dbAdminAnyDatabase", "db" : "admin" } ] } 
> 
[root@yq-mapp-otadb248 local]# 
[root@yq-mapp-otadb248 local]# /usr/local/mongodb/bin/mongo --port=27117 -u useradmin -p mongodb_020248_@JJMatch --authenticationDatabase admin 
MongoDB shell version: 3.2.5-20-g07e21d8 
connecting to: 127.0.0.1:27117/test 
Server has startup warnings: 
2016-10-19T12:12:59.096+0800 I CONTROL [initandlisten] 
2016-10-19T12:12:59.096+0800 I CONTROL [initandlisten] ** WARNING: The server was started without specifying a --bind_ip 
2016-10-19T12:12:59.096+0800 I CONTROL [initandlisten] ** and listens for connections on all available network interfaces. 
2016-10-19T12:12:59.097+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended. 
2016-10-19T12:12:59.097+0800 I CONTROL [initandlisten] 
2016-10-19T12:12:59.097+0800 I CONTROL [initandlisten] 
2016-10-19T12:12:59.097+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 
2016-10-19T12:12:59.097+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 
2016-10-19T12:12:59.097+0800 I CONTROL [initandlisten] 
2016-10-19T12:12:59.097+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 
2016-10-19T12:12:59.097+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 
2016-10-19T12:12:59.097+0800 I CONTROL [initandlisten] 
2016-10-19T12:12:59.097+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 2048 processes, 8192 files. Number of processes should be at least 4096 : 0.5 times number of files. 
2016-10-19T12:12:59.097+0800 I CONTROL [initandlisten] 
> 
> use admin; 
switched to db admin 
> 
> db.system.users.find(); 
{ "_id" : "admin.useradmin", "user" : "useradmin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "6hLx/d97hS+yfoN47QTmXQ==", "storedKey" : "B0PqwVs3GFKIHQyyQ6mBp1MA370=", "serverKey" : "xK53AKKAvFCdn5rsEtij5QB9RtU=" } }, "roles" : [ { "role" : "clusterAdmin", "db" : "admin" }, { "role" : "userAdminAnyDatabase", "db" : "admin" }, { "role" : "dbAdminAnyDatabase", "db" : "admin" } ] 
> 
>

6. 為運用創立營業賬號

創立一個 admin / admin 用戶,具有可以自行創立用戶和數據庫的權限,暗碼營業本身再修正:

db.createUser({user:'admin',pwd:'admin',roles:[{role:'userAdminAnyDatabase',db:'admin'},{role:'dbAdminAnyDatabase',db:'admin'}]})

驗證營業賬號上岸:

/usr/local/mongodb/bin/mongo --port=27117 -u admin -p admin --authenticationDatabase admin

該步調,現實操作進程以下:

> 
> db.system.users.find().pretty() 
{ 
"_id" : "admin.useradmin", 
"user" : "useradmin", 
"db" : "admin", 
"credentials" : { 
"SCRAM-SHA-1" : { 
"iterationCount" : 10000, 
"salt" : "6hLx/d97hS+yfoN47QTmXQ==", 
"storedKey" : "B0PqwVs3GFKIHQyyQ6mBp1MA370=", 
"serverKey" : "xK53AKKAvFCdn5rsEtij5QB9RtU=" 
} 
}, 
"roles" : [ 
{ 
"role" : "clusterAdmin", 
"db" : "admin" 
}, 
{ 
"role" : "userAdminAnyDatabase", 
"db" : "admin" 
}, 
{ 
"role" : "dbAdminAnyDatabase", 
"db" : "admin" 
} 
] 
} 
{ 
"_id" : "admin.admin", 
"user" : "admin", 
"db" : "admin", 
"credentials" : { 
"SCRAM-SHA-1" : { 
"iterationCount" : 10000, 
"salt" : "snlJe16a2PX3dSwxnOsfAw==", 
"storedKey" : "VOoX1e7F0tOme6YuR+iyMLuEWK8=", 
"serverKey" : "aSlpI7TzlyJ5Ccbd8GoptNB8khk=" 
} 
}, 
"roles" : [ 
{ 
"role" : "userAdminAnyDatabase", 
"db" : "admin" 
}, 
{ 
"role" : "dbAdminAnyDatabase", 
"db" : "admin" 
} 
] 
} 
> 
> ^C 
bye 
[root@yq-mapp-otadb248 local]# 
[root@yq-mapp-otadb248 local]# 
[root@yq-mapp-otadb248 local]# 
[root@yq-mapp-otadb248 local]# /usr/local/mongodb/bin/mongo --port=27117 -u admin -p admin --authenticationDatabase admin 
MongoDB shell version: 3.2.5-20-g07e21d8 
connecting to: 127.0.0.1:27117/test 
> 
> show dbs 
admin 0.000GB 
local 0.000GB 
> 
>

7. 實例啟動時的報警處置

在mongodb啟動進程中,有時會提醒一些正告,個中罕見的兩類正告處置進程以下:

正告1:

提醒:

2016-10-19T12:12:59.097+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 2048 processes, 8192 files. Number of processes should be at least 4096 : 0.5 times number of files.

處置方法,依據提醒,將 processes 值,由如今的 2048,修正為4096,乃至更高的值。

ps -ef|grep mongod
cat /proc/32321/limits
cat /etc/security/limits.d/90-nproc.conf 
vim /etc/security/limits.d/90-nproc.conf

該步調現實操作進程以下:

[root@yq-mapp-otadb248 limits.d]# 
[root@yq-mapp-otadb248 limits.d]# cat /etc/security/limits.d/90-nproc.conf 
# 20160621 limit?? 
#* soft nproc 51200 
#* hard nproc 51200 
* soft nproc 2048 
* hard nproc 16384 
* soft nofile 8192 
* hard nofile 8192 
* soft stack 8192 
* hard stack 8192 
* soft memlock unlimited 
* hard memlock unlimited 
[root@yq-mapp-otadb248 limits.d]# vim /etc/security/limits.d/90-nproc.conf 
# 20160621 
#* soft nproc 51200 
#* hard nproc 51200 
* soft nproc 8192 
* hard nproc 16384 
[root@yq-mapp-otadb248 ~]# 
[root@yq-mapp-otadb248 ~]# ulimt -a 
-bash: ulimt: command not found 
[root@yq-mapp-otadb248 ~]# ulimit -a 
core file size (blocks, -c) 0 
data seg size (kbytes, -d) unlimited 
scheduling priority (-e) 0 
file size (blocks, -f) unlimited 
pending signals (-i) 30422 
max locked memory (kbytes, -l) unlimited 
max memory size (kbytes, -m) unlimited 
open files (-n) 8192 
pipe size (512 bytes, -p) 8 
POSIX message queues (bytes, -q) 819200 
real-time priority (-r) 0 
stack size (kbytes, -s) 8192 
cpu time (seconds, -t) unlimited 
max user processes (-u) 8192 
virtual memory (kbytes, -v) unlimited 
file locks (-x) unlimited

正告2:

2016-10-19T12:12:59.097+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-10-19T12:12:59.097+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-10-19T12:12:59.097+0800 I CONTROL [initandlisten] 
2016-10-19T12:12:59.097+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-10-19T12:12:59.097+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'

確認:

cat /sys/kernel/mm/transparent_hugepage/enabled
cat /sys/kernel/mm/transparent_hugepage/defrag

處置:

echo "never" > /sys/kernel/mm/transparent_hugepage/enabled
echo "never" > /sys/kernel/mm/transparent_hugepage/defrag

該步調,現實操作進程以下:

[root@yq-mapp-otadb248 limits.d]# cat /sys/kernel/mm/transparent_hugepage/enabled 
[always] madvise never 
[root@yq-mapp-otadb248 limits.d]# cat /sys/kernel/mm/transparent_hugepage/defrag 
[always] madvise never 
[root@yq-mapp-otadb248 limits.d]# 
[root@yq-mapp-otadb248 limits.d]# echo "never" > /sys/kernel/mm/transparent_hugepage/enabled 
[root@yq-mapp-otadb248 limits.d]# echo "never" > /sys/kernel/mm/transparent_hugepage/defrag 
[root@yq-mapp-otadb248 limits.d]# 
[root@yq-mapp-otadb248 limits.d]# cat /sys/kernel/mm/transparent_hugepage/enabled 
always madvise [never] 
[root@yq-mapp-otadb248 limits.d]# cat /sys/kernel/mm/transparent_hugepage/defrag 
always madvise [never] 
[root@yq-mapp-otadb248 limits.d]# 
[root@yq-mapp-otadb248 limits.d]#

8. 創立mongodb實例辦事治理劇本

為了便利對mongodb實例的啟動和停滯,可以先創立mongodb的啟動劇本,停滯劇本,然後在 /etc/init.d/ 目次下創立一個辦事劇本,如許便可以用 service 停止實例治理了。

劇本1:啟動劇本:

vim /home/mongodb/scripts/mongodb_start.sh

#!/bin/sh 
# the scripts is used to start mongodb instance with port 27117. 
# created by zhaofx on 20161019. 
echo -n "Starting MongoDB port 27117 ... " 
/usr/local/mongodb/bin/mongod --config /etc/mongodb/mongo_27117.conf &

劇本二:停滯劇本:

vim /home/mongodb/scripts/mongodb_stop.sh

#!/bin/bash 
# the scripts is used to stop mongodb instance with port 27117. 
# created by zhaofx on 20161019. 
echo -n "Stopping MongoDB port 27117" 
pid=`ps -o pid,command ax | grep mongod | awk '!/awk/ && !/grep/ {print $1}'`; 
if [ "${pid}" != "" ]; then 
kill -2 ${pid}; 
fi

劇本三:辦事劇本:

vim /etc/init.d/mongodb

vim /etc/init.d/mongodb 
#! /bin/sh 
# the scripts is used to mange mongodb service with linux service type. 
# created by zhaofx on 20161019. 
PATH=/usr/local/mongodb/bin:/sbin:/bin:/usr/sbin:/usr/bin 
NAME=mongodb 
start(){ 
/home/mongodb/scripts/mongodb_start.sh 
} 
stop(){ 
/home/mongodb/scripts/mongodb_stop.sh 
} 
test -x $DAEMON || exit 0 
set -e 
case "$1" in 
start) 
start 
;; 
stop) 
stop 
;; 
*) 
N=/etc/init.d/$NAME 
echo "Usage: $N {start|stop}" >&2 
exit 1 
;; 
esac 
exit 0

修正劇本的屬組,添加履行權限:

chown -R mongodb:mongodb /home/mongodb/scripts/ 
chown mongodb:mongodb /etc/init.d/mongodb 
chmod +x /home/mongodb/scripts/mongodb_start.sh 
chmod +x /home/mongodb/scripts/mongodb_stop.sh 
chmod +x /etc/init.d/mongodb

最初辦事啟動和停滯mongodb實例的進程為:

[root@yq-mapp-otadb248 ~]# 
[root@yq-mapp-otadb248 ~]# service mongodb stop 
Stopping MongoDB port 27117 
[root@yq-mapp-otadb248 ~]# 
[root@yq-mapp-otadb248 ~]# 
[root@yq-mapp-otadb248 ~]# 
[root@yq-mapp-otadb248 ~]# service mongodb start 
Starting MongoDB port 27117 ... [root@yq-mapp-otadb248 ~] 
about to fork child process, waiting until server is ready for connections. 
forked process: 36088 
child process started successfully, parent exiting 
[root@yq-mapp-otadb248 ~]#

以上所述是小編給年夜家引見的mongodb 3.2.5裝置進程具體記載,願望對年夜家有所贊助,假如年夜家有任何疑問請給我留言,小編會實時答復年夜家的。在此也異常感激年夜家對網站的支撐!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved