Note Server MongoDB Database Migration Actual Record [Cloud MongoDB Database Migration]

1. Actual record of server MongoDB database migration

1. Background:

My original server expired and I could only purchase a server as a new user due to the high cost.

2. Requirements:

Migrate the original cloud MongoDB database to the newly purchased server.

3. Note:

The previous MongoDB did not use the visual creation of the pagoda, but used the command line, resulting in the visualization page not being able to see the database. So use the backup command to back up and download the backup to the local computer.

1.1 Backup MongoDB database steps

1. Connect to the MongoDB server. You can connect to a MongoDB server using the mongo command. For example, enter the following command at the command line:

mongo --host <hostname> --port <port> -u <username> -p <password> --authenticationDatabase admin

Among them, "<hostname>" specifies the host name or IP address of the MongoDB server, "<port>" specifies the port number of the MongoDB server, "<username>" and "<password>" specify the username and password of the MongoDB administrator, " --authenticationDatabase" specifies the name of the database where the MongoDB administrator resides.

2. Select the database to backup. After connecting to the MongoDB server, use the "use" command to select the database to backup. For example:

use mydatabase

3. Execute the backup command. Use the mongodump command to back up the MongoDB database. For example:

mongodump --out /path/to/backup/folder/

Among them, the "-out" parameter specifies the directory where the backup file is saved.

4. Complete the backup. The backup process may take some time, depending on the size of the data you are backing up and the performance of your MongoDB server. When the backup is complete, you should find a folder containing the backup data in the directory where the backup file was saved.

Go to the directory where the backup file is saved, and download it to the local computer:

insert image description here

1.2 Restore the database to the new server

1. Use pagoda visualization to create a new database. Set the database name, username and password.

image-20230407120408566

2. Upload the backup MongoDB data in the local computer to the new server:

image-20230407120528962

3. Use terminal commands to perform recovery operations: (first turn off database password authentication)

Use the mongorestore command in the SSH terminal to restore the data in the backup file to the new database. For example, assuming your backup file is named "mydb.bson", you can restore the data using the following command:

mongorestore -h 127.0.0.1 -d <database_name> <path_to_backup_file>

Take mine as an example:

mongorestore -h 127.0.0.1 -d <数据库名> /www/backup/database/mongodb/beifeng/mongo

Among them, the "-h" parameter specifies the IP address of the MongoDB server. If the MongoDB server is installed on the local machine, you can use "127.0.0.1" or "localhost"; the "-d" parameter specifies the name of the database to be restored, replace it with your Created database name; "<path_to_backup_file>" specifies the path and name of the backup file, such as "/home/wwwroot/default/_backup/database/mydb.bson".

The execution effect is as follows:

image-20230407120701071

4. Use Navicat Premium to remotely connect and use to view:

image-20230407120807693

The data recovery is complete and can be used normally.

1.3 Summary

1. The general steps to back up the MongoDB database are as follows:

  1. Connect to the MongoDB server.
  2. Select the databases to back up.
  3. Use the mongodump command to back up the MongoDB database.
  4. Complete the backup.

During the backup process, it is recommended to perform backups during non-working hours, and avoid other operations on the MongoDB server during the backup period to avoid impact on server performance.

2. The following are the general steps to restore a MongoDB database:

  • Connect to the MongoDB server. You can connect to a MongoDB server using the mongo command. For example, enter the following command at the command line:
cssCopy code
mongo --host <hostname> --port <port> -u <username> -p <password> --authenticationDatabase admin

Among them, "" specifies the host name or IP address of the MongoDB server, "" specifies the port number of the MongoDB server, "" and "" specify the username and password of the MongoDB administrator, and "--authenticationDatabase" specifies the name of the database where the MongoDB administrator resides.

  • Create the database to restore. If the database to be restored does not exist on the MongoDB server, it needs to be created first. For example, after connecting to the MongoDB server, use the "use" command to select an existing database, and then use the "db.createCollection()" command to create a new collection. For example:
perlCopy codeuse mydatabase
db.createCollection("mycollection")
  • Execute the restore command. Use the mongorestore command to restore the MongoDB database. For example:
bashCopy code
mongorestore --dir /path/to/backup/folder/

Among them, the "-dir" parameter specifies the directory where the backup file is located.

  • Complete recovery. The restore process may take some time, depending on the size of the data you are restoring and the performance of your MongoDB server. When the restore is complete, you should be able to find all the data in the restored database.

Please note that when restoring a MongoDB database, if data already exists in the target database, the restoration operation may cause data conflicts or data loss. Therefore, it is recommended to back up the target database and clear the target database before the recovery operation. At the same time, in order to ensure the correctness of the recovery operation, it is recommended to ensure that the MongoDB server is running normally and no other users are operating the MongoDB server during the recovery operation.

Guess you like

Origin blog.csdn.net/weixin_52908342/article/details/130009265