MongoDB upgrade from 3.6 to 4.0: How to fix the "collection does not have uuid in kvcatalog" exception? - mongodb

I run a MongoDB replica set in a containerization environment orchestrated by Rancher.
After rolling out the latest update, MongoDB as been upgraded from 3.6.x to 4.0.x (latest).
No upgrade path has been followed and, as a result, when trying to bind the volume holding the database storage, the service fails to start, exiting with the following exception:
STORAGE [initandlisten] exception in initAndListen: MustDowngrade: Collection does not have UUID in KVCatalog. Collection: admin.system.version, terminating
I get the same outcome even attempting to launch mongod with the --repair option.
I have also tried to rollback the container to MongoDB 3.6.16, but the journal version is now incompatible.
We don't have snapshots of the volume, hence restoring the data is not an option (this is not a production environment). The only solution I've found online is not applicable, since it suggests to leverage mongodump and mongorestore, requiring a running database with the data storage attached.
I'm running out of ideas, any advice about how to fix this?

A bug report on this issue recommended following the full upgrade path and that worked for me. I was able to go back to my old version when I ran into this error, and then upgrade properly.
Specifically: upgrade just one major version at a time and check the prerequisites and upgrade steps carefully. The most important part of this is checking your compatibility with db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } ) and make sure it matches the current version before upgrading to the next version. Update it with command like db.adminCommand( { setFeatureCompatibilityVersion: "3.6" } )
Read the full docs at pages like https://docs.mongodb.com/manual/release-notes/3.6/#upgrade-procedures https://docs.mongodb.com/manual/release-notes/4.0/#upgrade-procedures etc

I was updating MongoDB from 3.6 to 4.2 with Homebrew on Mac OS, and ran into this similar situation.
Since formula mongodb was removed from homebrew-core, the team of mongodb maintained a custom Homebrew tap.
Following steps is my solution for upgrading MongoDB from 3.6 to 4.2 with Homebrew:
Uninstall old version mongodb from Homebrew
# If you run mongodb in background service, stop it first.
brew services stop mongodb
# If you have multiple version of mongodb, you might need to add `--force` option for deleting all of them at once.
brew uninstall mongodb
Install new mongodb from custom Homebrew tap
brew tap mongodb/brew
brew install mongodb-community#3.6
Add mongodb-community#3.6 to your PATH
echo 'export PATH="/usr/local/opt/mongodb-community#3.6/bin:$PATH"' >> ~/.bash_profile
Restart your terminal, and run mongod
Open another tab or terminal windows to run mongodump
Terminate mongod
Uninstall mongodb-community#3.6 and reinstall mongodb-community
brew uninstall mongodb-community#3.6
brew install mongodb-community
Delete old data with old mongo data structure
rm -rf /data/db/*
Run mongod
Open another tab or terminal windows to run mongorestore dump/
Once mongorestore completed, mongod will be terminated.
And you can run mongodb as a background service or just run mongod again. Open MongoDB with mongo or Robo 3T(formerly Robomongo), you will see your data is good as usual.
Hope it helps.

Alright after about 3hours of trying to fix exactly what you are describing, here are the steps I took. Here I will show how to retain the data while downgrading to 3.6 and also how to update it 4.0 with the retained data.
The reason this happens for mac users is because there was an update for people using brew can no longer download Mongodb directly from brew. Then people tend to download the newer community edition instead from brew which then gets in conflict the old version of 3.6 db folder still present in the directory. Instead you would need to download it from the actual mongodb website:
https://www.mongodb.com/download-center/community
Here what you would want to do, is download everything manually. Follow these instructions here, but do this with the 3.6 version:
https://medium.com/#saurabhkumar_4718/install-mongodb-without-homebrew-on-mac-os-2a98b68ab09c
NOTE: when you copy .bash_profile please make sure export commands are not
Once you have successfully installed 3.6 manually you can mongodump to be safe, and or export all your mongo data into json with mongoexport for any data that you must really really save.
After that you can fresh install 4.0 manually again by following exactly the same steps as previously done for 3.6
This worked for me without having to do any 'mongorestore' as the 'db' folder was left alone and all my data was there, but maybe extra steps might be needed to restore the data. Worst comes to worst you have .jsons that needs to be rewritten. 'mongo --version' also showed the right version :) Hope it helps

Backing-up all the databases (using mongodump)
Removing every file in /data/db, due to their incompatibility with the newer version: rm -rf /data/db/*
Restarting the MongoDB service by mongod
Restoring all the databases (using mongorestore)
https://dba.stackexchange.com/a/252393

For Docker:
I had this issue when I was upgrading a project that used Docker from 3.6 to 4.2 (and 4.4).
The way the project is set up means I don't have a physical /data/db directory to purge.
However, there is a volume defined that acts as the /data/db directory.
In this case all you need to do is backup any data you need and remove the associated volume.
If using vs code with the docker extension, you can simply open your docker tab in the sidebar, expand the volumes section, locate the volume in question (for me it was called mongo-4-2-upgrade-test_mongo-data which depends on how your various containers are named for your project), right-click and remove.
Ensure that the containers that use this volume have been properly shut down or you'll get a 409 conflict error. I did the same as above but found my mongo container (in the containers section of the docker tab) instead. then when I restarted my docker-compose file the new version of mongo was installed and ran as expected.

Related

How to uninstall MongoDB that was manually installed

I installed MongoDB manually on the MongoDB website. https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/
I followed the instructions under "Install MongoDB Community Edition Manually".
How do I uninstall MongoDB completely? I have searched around and only found how to uninstall using Homebrew. I did not install MongoDB using Homebrew.
Any help is greatly appreciated.
Just follow the manual install steps in reverse order:
shutdown mongod
remove your data directory (if you no longer want this)
remove the directory with the MongoDB binaries
remove the directory with the MongoDB binaries from your shell PATH
Other files that might have been created depending on how you ran MongoDB:
there may be old log files to remove if you specified a --logpath outside your data directory
there may be a launchctl script and configuration file to remove if you configured mongod to run as a service

Mongodb#3.2 shell on MacOS

I first ran brew install mongodb, which successfully completed and installed version 3.4.4. I was then able to start the server using brew services start mongodb, and access the shell via running mongo.
Then I discovered I need mongodb version 3.2.0. I uninstalled mongo v3.4.4 via brew uninstall mongodb and verified service was gone and bin files removed.
I then ran brew search mongodb and found mongodb#3.2 in the list, and so ran brew install mongodb#3.2. This completed successfully, and I was able to start the server using brew services start mongodb#3.2, which is successful. I then go to access the shell via 'mongo', or 'mongo#3.2' or 'mongo32' and none of these exist.
What should this bin file be named and why can't I find it? with v3.4.4 shell bin executable was /usr/local/bin/mongo - this file doesn't exist after installing 'mongodb#3.2'.
I see it in /usr/local/Cellar/mongodb#3.2/3.2.11/bin/ along with all the other binaries.
I don't know why it isn't symlinked to /usr/local/bin/ like most other programs.

Install ONLY mongo shell, not mongodb

As mentioned above, I need to install only the mongo shell on a RHEL instance (machine A).
I have a mongodb server on a separate instance (machine B) and need to connect to that from A to run mongodump and mongorestore commands.
I tried looking it up on the web but all I got was instructions to install the complete mongodb package.
Any help appreciated.
Official documentation says that you should be fine installing mongodb-org-shell only.
Create a repository file at sudo vim /etc/yum.repos.d/mongodb.repo
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
gpgcheck=1
enabled=1
sudo yum clean all
sudo yum install mongodb-org-shell-4.0.2
mongo <mongo-server-ip> Note: Step 4 will not work in all cases. You need to check proper command to connect to remote mongo server.
Source: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/
You can follow the official tutorial on installing mongodb, and instead of installing the main package that will install all the packages, just install the package containing mongoshell
Please follow below steps.
Create a file /etc/yum.repos.d/mongodb.repo
Update the above file to contain:
[mongodb]
name=MongoDB Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/
gpgcheck=0
enabled=1
sudo yum install -y mongodb-org-shell-2.6.9
mongo mongo-db_server_IP:27017/database_name -u username -p password
You might want to install mongosh (MongoDB Shell) instead. The legacy mongo shell has been deprecated since MongoDB v5.0 and replaced by mongosh. From the mongosh docs:
The new MongoDB Shell, mongosh, offers numerous advantages over the
legacy mongo shell, such as:
Improved syntax highlighting.
Improved command history.
Improved logging.
Currently mongosh supports a subset of the mongo shell
methods. Achieving feature parity between mongosh and the mongo shell
is an ongoing effort.
To maintain backwards compatibility, the methods that mongosh supports
use the same syntax as the corresponding methods in the mongo shell.
To see the complete list of methods supported by mongosh, see MongoDB
Shell Methods.
Further reference: Compatibility Changes with Legacy mongo Shell
Head to Mongo download page; download your required package regarding your operating system. Finally follow the instruction of your operating system to install the package.
Especially to install mongo or mongosh:
download .tgz file and untar the file into /usr/local/
add /usr/local/mongo/ into the shell profile (e.g. ~/.bashrc) and restart your terminal
For more information read the docs.
P.S.: mongo in the path should be vary based on the version of the downloaded package.

when i upgrade mongo it still says version 2.x.x , how can i go to a higher version?

How do I upgrade from mongo version 2 to version 3 ?
I am trying to upgrade from mongo 2.x.x to the latest version (which at the time is 3.2.x).
The official (3.2 upgrade) instructions say I need to go to version 3.0.x first and so that is all i'm trying to achieve right now - to get from version 2 to version 3.
First try : apt-get
I first tried using apt-get, following these instructions (linked to from this page, which was linked to from these official (3.0 upgrade) instructions).
That all appeared to work alright, however my version ($ mongo --version) number didn't change, so no upgrade actually happened at all.
Second try : mongod-10gen
I then found these instructions and followed them.
This time, again, every step was successful but the end result was that my version has now gone down! (from 2.6.1 to 2.4.14)
I've also tried following the instructions in this SO answer, which are essentially the same as the other instructions - and gave the same result - success on every step, but the final result is still a v2 mongo.
Third try : copy the binaries
The official (3.0 upgrade) instructions actually first suggest downloading and copying over the binaries (but I tried the auto install instructions first because that seemed easier).
My binaries are all actually directly in /usr/bin/.
Get the url for the binaries for your system (for the curl command below) from the download binaries page.
(For me it was for Ubuntu 14, for previous release, v3.0.14)
Then download the binaries, and copy them directly into /usr/bin/.
Like so:
$ curl https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1404-3.0.14.tgz -o mongo_3.0.14.tgz
$ tar -xvf mongo_3.0.14.tgz
$ sudo service mongod stop # stop DB
$ sudo cp mongodb-linux-x86_64-ubuntu1404-3.0.14/bin/* /usr/bin
$ sudo service mongod start # and restart
$ mongo --version
MongoDB shell version: 3.0.14
Success!!
So, the correct approach was to follow the initial instructions - however it was not obvious to me what "copy the binaries" meant at first, and the alternative auto install with apt-get I really thought would be the simple and right way to go (why doesn't that work??) - I suspect others will come across this problem so I will put this third try as an answer and still post the question

How do I install older version of postgresql and postgis?

I am new to Linux and I found it really difficult to install older version of postgresql (along with postgis). I have uninstalled everything I could find connected to postgresql in aptidude. As only the newest version is available through apt-get install, I have went through several tutorials how to get 9.1 installed.
Example: https://wiki.postgresql.org/wiki/Apt (with 9.1 at the end, not 9.3)
After I was done with this tutorial, pgAdmin III is still showing 9.3 as version.
What's the correct way to dump 9.3 and install 9.1 as well as postgis 1.5?
I am using Mint 17
Okay. First off it is perfectly fine to run multiple versions of postgresql alongside one another. You can have more than one cluster on a box, and each cluster can run different versions. The debian pacakges are very well designed to make this easy to do.
However since you've indicated you're a newby to linux/postgresql it's probably best to get 9.3 completely cleaned from your system to avoid frustrating hiccups which will take you a long time to figure out.
To do this you need to:
Purge all postgresql 9.3 packages from dpkg
Manually delete any 9.3 data and configuration for 9.3 cluster
Make sure you've created a 9.1 cluster
Step 1 – purge DPKG
DPKG manages the packages on your system. APT acts as a mechanism for getting packages to install. A lot of DPKG functionality is available through apt, but some things are just easier on dpkg. In your comments you've shown that DPKG is still showing remnants of 9.3. When you uninstall a package some parts of it may hang around (such as configuration files). You can remove multiple packages in one command. To get rid of these completely you can tell dpkg to purge them dpkg --purge <package name>. You can remove multiple packages in one command and it's best to do so.
dpkg --list will list everything on your system. dpkg --list | grep postgresql searches this list for postgresql.
Step 2 – Manually delete data and configuration from the old 9.3 cluster.
When you first install postgresql it creates a cluster for you; no questions asked. This trips up a lot of new users because they neither knew that it happened nor how to replace it. Now that 9.3 is uninstalled you can go ahead and simply delete the configuration and data:
If there was anything you wanted to keep in the installed 9.3 database DO NOT run the following commands
sudo rm -rf /etc/postgresql/9.3 Removes the configuration.
sudo rm -rf /var/lib/postgresql/9.3 Removes the data.
Step 3 – Create a new cluster
You've indicated in comments that postgresql 9.1 is installed. Before you run this command it's worth reading the manual. man pg_createcluster.
sudo pg_createcluster 9.1 <think up a name>
Now you should be able to start up postgresql sudo service postgresql start
Edit
If pg_config is still listing the incorrect version then... this can be a result of remnants of 9.3 still left on your system. This may not be a problem. To allow multiple versions to run along side one another pg_config is a wrapper script which searches for the latest (highest version) of /usr/lib/postgresql/*/bin/pg_config. So what you're seeing is the result of running /usr/lib/postgresql/9.3/bin/pg_config. If 9.3 is completely cleaned from your system then /usr/lib/postgresql/9.3 should have gone.
If you want to see the configuration of 9.1 then try running:
/usr/lib/postgresql/9.1/bin/pg_config.
The wrapper scripts which starts the server will use the file structure of /etc/postgresql and use the apropriate version. So /etc/postgresql/9.1/main will startup version as 9.1.