Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mongdb replica set building

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

One: there are two common methods to install mongodb: I use method 2:

Method 1 install the package, download the .tgz file directly, and then decompress it:

Https://www.mongodb.com/download-center/enterprise # download the required version

Download percona for mongodb

Https://www.percona.com/downloads/percona-server-mongodb-LATEST/tar-zxvf mongodb-linux-x86_64-3.0.6.tgz # extract mv mongodb-linux-x86_64-3.0.6 / / usr/bin/mongodb/bin/mongod # copy the unzipped package to the specified directory

This startup method is as follows:

/ usr/bin/mongodb/bin/mongod-f / etc/mongod.conf

Method 2 yum installation, and then the default configuration file is / etc/mongod.conf, so install the mongodb

The related commands are all under / usr/bin!

Install yum feeds (percona's own yum feeds)

# yum-y install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm

Yum list | grep mongo # Select the appropriate version

[root@beijing-fuli-hadoop-02 bin] # yum install Percona-Server-MongoDB-34-mongos.x86_64

How this method starts:

Service mongod start

Second: generate keyfile

[root@beijing-fuli-hadoop-02 mongodb] # openssl rand-base64 756 > / data/mongodb/config/mongodb.key

Three: write configuration files

1. Note that the space and case of the file will cause startup failure

two。 If clusterAuthMode is configured, be sure to generate keyfile in advance

3. With regard to the priority setting of nodes, the Secondary with higher Priority is relative to the Secondary with lower Priority.

The election will be initiated earlier, and it will be easier to become the new Primary node, but even if the Secondary with low priority

It can also be elected as a Primary node within a period of time, and the point is, when this happens, the replica set will continue.

Conduct elections until the highest available priority Secondary is elected as Primary, so when you want a specific

When the Secondary node is the master, you need to configure a different Primary, otherwise you only need to configure it to the same

Priority (for example, all are 1)

4. About the setting of vote: from mongodb 3.2onwards, non-voting members must configure the Priority to be 0Justice priority greater than

Members of 0 must have their vote configured as 1

4: if you want to start mongodb with a non-root, then create an account and affiliated group for mongodb

[root@beijing-fuli-hadoop-02 init.d] # groupadd-g 666 mongod

[root@beijing-fuli-hadoop-02 init.d] # useradd-m-s / bin/bash-g mongod-u 666 mongod

Five: start the database: all three nodes are started

[mongod@beijing-fuli-hadoop-02 db] $/ usr/bin/mongodb/bin/mongod-f / etc/mongod.conf

About to fork child process, waiting until server is ready for connections.

Forked process: 89234

Child process started successfully, parent exiting

Six: switch to replication set architecture

> use admin # switch to admin database switched to db admin # initialize replica set. The replica set name is liuhe_rs The first member is himself, and then he will be elected as the host > rs.initiate ({_ id:'liuhe_rs',members: [{_ id: 0, host: "10.9.21.178id 27017"}, {_ id: 1, host: "10.9.21.179id 27017"}, {_ id: 2, host: "10.9.21.114id 27017"}]}) {"ok": 1}

Or as follows:

> config= {_ id:'liuhe_rs',members: [{_ id: 0, host: "10.9.21.178id 27017"}, {_ id: 1, host: "10.9.21.179Freight 27017"}, {_ id: 2, host: "10.9.21.114id 27017"}]}

> rs.initiate (config)

Seven: start the error report:

7.1Parameter indexBuildRetry cannot be configured in replica set

[root@beijing-fuli-hadoop-02 mongodb] # / usr/bin/mongodb/bin/mongod-f / etc/mongod.conf

2019-11-11T22:36:15.551+0800 F CONTROL [main] Failed global initialization: BadValue: replication.replSetName is not allowed when storage.indexBuildRetry is specified

Error content:

Failed global initialization: BadValue: replication.replSetName is not allowed when storage.indexBuildRetry is specified

The storage parameter indexBuildRetry cannot coexist with the replica set, and the indexBuildRetry parameter needs to be annotated when the replica set is opened. Official website link

Changed in version 4.0: The setting storage.indexBuildRetry cannot be used in conjunction withreplication.replSetName.

7.2. Start error reporting error number 100

At startup, the appropriate initialization library and specific problems are generated in the data directory, so

Make sure this directory is empty.

[mongod@beijing-fuli-hadoop-02] $/ usr/bin/mongodb/bin/mongod-f / etc/mongod.conf

About to fork child process, waiting until server is ready for connections.

Forked process: 89082

ERROR: child process failed, exited with error number 100

To see additional information in this output, start without the "--fork" option.

It may be because there are other files in your database directory, just empty it!

7.3Boot error: error number 14

[mongod@beijing-fuli-hadoop-04 log] $/ usr/bin/mongodb/bin/mongod-f / etc/mongod.conf

About to fork child process, waiting until server is ready for connections.

Forked process: 100524

ERROR: child process failed, exited with error number 14

To see additional information in this output, start without the "--fork" option.

Check the log and find as follows:

2019-11-16T16:51:37.938+0800 E NETWORK [initandlisten] Failed to unlink socket file / tmp/mongodb-27017.sock Operation not permitted

It turns out that it was started with a root account to generate a mongodb-27017.sock file for root users, and then use mongod

The user starts, but does not have the permission to operate. Delete this file, and then start it with the mongod user will automatically generate a

The files that belong to mongod are as follows:

[root@beijing-fuli-hadoop-02 tmp] # ll mongodb-27017.sock

Srwx- 1 mongod mongod 0 Nov 16 17:12 mongodb-27017.sock

So what does the mongodb-27017.sock file do?

The Unix socket file that the server communicates with the local client.

It opens when it starts.

The default storage path is / tmp/mongodb-.sock

Eight: the basic maintenance operation command of mongodb:

8.1: view the status of the mongodb replica set

MongoDB Enterprise liuhe_rs:PRIMARY > rs.status ()

8.2: allow secondary nodes to write

The secondary node in replica set is unreadable by default. In the application of writing more and reading less, Replica Sets is used to achieve read-write separation. By specifying the slaveOk at connection time or in the main library, the Secondary shares the read pressure, and the Primary only undertakes the write operation

The first method: db.getMongo (). SetSlaveOk (); # executes from the library and is temporarily initiated

The second method: rs.slaveOk (); # execute from the library and start temporarily

Both of the above methods work temporarily. The next time you enter the instance through mongo, the query will still report an error. You can do this in the following ways

[mongod@beijing-fuli-hadoop-04 ~] $vi .mongorc.js

Rs.slaveOk ()

In this way, you can query it every time you enter it through the mongo command.

8.3. View the library to which the mongodb is currently connected

> use liuwenhe

Switched to db liuwenhe

> db.stats ()

{

"db": "liuwenhe"

"collections": 1

"views": 0

"objects": 1

"avgObjSize": 48

"dataSize": 48

"storageSize": 16384

"numExtents": 0

"indexes": 1

"indexSize": 16384

"fsUsedSize": 18297913344

"fsTotalSize": 75949613056

"ok": 1

}

8.4 View replica set slave library status information (delay, members, etc.)

MongoDB Enterprise liuhe_rs:PRIMARY > rs.printSlaveReplicationInfo ()

Source: 10.9.21.179:27017

SyncedTo: Sat Nov 16 2019 21:56:29 GMT+0800 (CST)

0 secs (0 hrs) behind the primary

Source: 10.9.21.114:27017

SyncedTo: Sat Nov 16 2019 21:56:29 GMT+0800 (CST)

0 secs (0 hrs) behind the primary

As shown above, there are two slave libraries, 10.9.21.179 and 10.9.21.114, which are then synchronized to the oplogs of the master library

Sat Nov 16 2019 21:56:29 time, no delay

8.5: view the oplog information of the replica set:

MongoDB Enterprise liuhe_rs:PRIMARY > rs.printReplicationInfo ()

Configured oplog size: 51200MB

Log length start to end: 424168secs (117.82hrs)

Oplog first event time: Tue Nov 12 2019 00:10:41 GMT+0800 (CST)

Oplog last event time: Sat Nov 16 2019 22:00:09 GMT+0800 (CST)

Now: Sat Nov 16 2019 22:00:16 GMT+0800 (CST)

8.6 View the users under a library:

MongoDB Enterprise liuhe_rs:PRIMARY > use admin

Switched to db admin

MongoDB Enterprise liuhe_rs:PRIMARY > show users

Nine: create an administrator user

Use admin

Db.createUser (

{

User: "liuwenhe"

Pwd: "liuwenhe"

Roles: [{role: "root", db: "admin"}]

}

)

MongoDB Enterprise liuhe_rs:PRIMARY > db.auth ('liuwenhe','liuwenhe')

Log in to the database:

Mongo-u liuwenhe-p liuwenhe-- authenticationDatabase admin

Create an ordinary account:

Db.createUser ({user:'user', pwd:'123456', roles: [{role:'readWrite', db:'liuwenhe'}]})

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report