29 Nov 2017, 14:50

Error When Importing Into Discourse: 'Can't connect to MySQL server'

I’ve recently been migrating an existing bbPress forum to Discourse, and ran into some trouble. There are good instructions for this on the Discourse meta forum, but once I got to the step of actually running the import script, it would fail on me:

root@discourse-2gb-nyc3-01-import:/var/www/discourse# su discourse -c "bundle exec ruby script/import_scripts/bbpress.rb"
loading existing groups...
loading existing users...
loading existing categories...
loading existing posts...
loading existing topics...
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/mysql2-0.4.9/lib/mysql2/client.rb:89:in `connect': Can't connect to MySQL server on 'xxx.xx.x.x' (113) (Mysql2::Error)
		from /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/mysql2-0.4.9/lib/mysql2/client.rb:89:in `initialize'
		from script/import_scripts/bbpress.rb:27:in `new'
		from script/import_scripts/bbpress.rb:27:in `initialize'
		from script/import_scripts/bbpress.rb:410:in `new'
		from script/import_scripts/bbpress.rb:410:in `<main>'

The error was Can't connect to MySQL server. I checked to see that the MySQL container was running (for me that meant running docker ps to see what’s listed), and though I had started it previously, it was down now. I started it up and tried again. Same problem. Checked the MySQL container and once again it had gone down. I tried simply running the MySQL container and not even attempting to run the migration script. The container would only run for a few seconds.

After some research, I learned that due to the fact that:

  1. My VPS instance was relatively low-memory and
  2. The MySQL docker image was quite memory-intensive

I simply didn’t have enough power to be running the MySQL container. Fortunately, this situation is where VPS-providers like DigitalOcean shine: I just shut down the server, went to the DigitalOcean web backend, bumped up my Droplet to about 4x the memory, then went back in and ran the migration, which worked like a charm this time. As soon as everything was done, I went back down to the amount of memory Discourse recommends to run a small forum.