# Ruby mysql2 error on migration

**URL:** https://meta.discourse.org/t/ruby-mysql2-error-on-migration/63157
**Category:** Support
**Created:** [May 22, 2017, 8:12pm UTC](https://meta.discourse.org/t/ruby-mysql2-error-on-migration/63157 "2017-05-22T20:12:49Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![kodab](https://avatars.discourse-cdn.com/v4/letter/k/45deac/32.png) [@kodab](https://meta.discourse.org/u/kodab)
#### Post date: [May 22, 2017, 8:12pm UTC](https://meta.discourse.org/t/ruby-mysql2-error-on-migration/63157/1 "2017-05-22T20:12:49Z")

</div>

Hi guys,

I’m trying to ue the [vanilla\_mysql.rb](https://github.com/discourse/discourse/blob/master/script/import_scripts/vanilla_mysql.rb) importer script to migrate my shitty forum to the awesome discourse platform. 🙂 However, when I execute the commands this happens:

**root@discourse-app:/var/www/discourse# gem install mysql2 --platform=ruby**  
Building native extensions. This could take a while…  
Successfully installed mysql2-0.4.6  
1 gem installed

**root@discourse-app:/var/www/discourse# bundle exec ruby script/import\_scripts/vanilla\_mysql.rb**  
script/import\_scripts/vanilla\_mysql.rb:1:in `require': cannot load such file -- mysql2 (LoadError) from script/import_scripts/vanilla_mysql.rb:1:in `’  
root@discourse-app:/var/www/discourse#

* * *

What am I doing wrong / how can I fix it? ☹ I’m using the default docker container on a clean ubuntu 16.04 vps.

---

<div class="post-metadata">

### Author: ![gerhard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gerhard/32/119479_2.png) [@gerhard](https://meta.discourse.org/u/gerhard)
#### Post date: [May 22, 2017, 8:46pm UTC](https://meta.discourse.org/t/ruby-mysql2-error-on-migration/63157/2 "2017-05-22T20:46:52Z")

</div>

Try this inside the docker container:

```bash
apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y libmysqlclient-dev
su discourse -c 'export IMPORT=1; bundle install --no-deployment --without test --without development --path vendor/bundle'
su discourse -c 'export IMPORT=1; bundle exec ruby script/import_scripts/vanilla_mysql.rb'

```

---

<div class="post-metadata">

### Author: ![kodab](https://avatars.discourse-cdn.com/v4/letter/k/45deac/32.png) [@kodab](https://meta.discourse.org/u/kodab)
#### Post date: [May 23, 2017, 10:26am UTC](https://meta.discourse.org/t/ruby-mysql2-error-on-migration/63157/3 "2017-05-23T10:26:04Z")

</div>

Thanks for your reply. Here is a short version of my output:

```
**root@discourse-app:/var/www/discourse#: su discourse -c 'export IMPORT=1; bundle install --no-deployment --without test --without development --path vendor/bundle'**
[[....]]
An error occurred while installing mysql2 (0.4.6), and Bundler cannot
continue.
Make sure that `gem install mysql2 -v '0.4.6'` succeeds before bundling.

**root@discourse-app:/var/www/discourse#: exec ruby script/import_scripts/vanilla_mysql.rb'** Could not find gem 'mysql2' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.

**root@discourse-app:/var/www/discourse#: bundle install**
[[....]]
Bundle complete! 96 Gemfile dependencies, 175 gems now installed.
Gems in the group development were not installed.
Bundled gems are installed into ./vendor/bundle.

**root@discourse-app:/var/www/discourse#: su discourse -c 'export IMPORT=1; bundle exec ruby script/import_scripts/vanilla_mysql.rb'** Could not find gem 'mysql2' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.

```

😕

---

<div class="post-metadata">

### Author: ![gerhard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gerhard/32/119479_2.png) [@gerhard](https://meta.discourse.org/u/gerhard)
#### Post date: [May 23, 2017, 10:31am UTC](https://meta.discourse.org/t/ruby-mysql2-error-on-migration/63157/4 "2017-05-23T10:31:57Z")

</div>

You need to install a dependency for it to work. I guess it’s missing.

```bash
apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y libmysqlclient-dev

```

---

<div class="post-metadata">

### Author: ![kodab](https://avatars.discourse-cdn.com/v4/letter/k/45deac/32.png) [@kodab](https://meta.discourse.org/u/kodab)
#### Post date: [May 27, 2017, 3:38pm UTC](https://meta.discourse.org/t/ruby-mysql2-error-on-migration/63157/5 "2017-05-27T15:38:34Z")

</div>

Thank you so mutch Gerhard! 🙂 It worked, I owe you.

---

<div class="post-metadata">

### Author: ![pgr](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pgr/32/156783_2.png) [@pgr](https://meta.discourse.org/u/pgr)
#### Post date: [November 12, 2018, 10:01am UTC](https://meta.discourse.org/t/ruby-mysql2-error-on-migration/63157/6 "2018-11-12T10:01:52Z")

</div>

Hi

I am facing this same situation but I am having trouble issuing these `su discourse -c` commands, they ask me for a password. I don’t think I created this user, it comes with the Docker container, so how do I get its password? Or how do I avoid being asked for one?

Thanks!

---

<div class="post-metadata">

### Author: ![Stephen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/stephen/32/95011_2.png) [@Stephen](https://meta.discourse.org/u/Stephen)
#### Post date: [November 12, 2018, 1:49pm UTC](https://meta.discourse.org/t/ruby-mysql2-error-on-migration/63157/7 "2018-11-12T13:49:42Z")

</div>

Is this a development install, an install [from the standard guide](https://github.com/discourse/discourse/blob/master/docs/INSTALL-cloud.md), or something else?

If it’s from the standard guide you shouldn’t be prompted for a password when using `su discourse -c` from within the container.

---

<div class="post-metadata">

### Author: ![pgr](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pgr/32/156783_2.png) [@pgr](https://meta.discourse.org/u/pgr)
#### Post date: [November 12, 2018, 3:09pm UTC](https://meta.discourse.org/t/ruby-mysql2-error-on-migration/63157/8 "2018-11-12T15:09:14Z")

</div>

Thanks, your confirmation that I “shouldn’t be prompted” sent me searching again, this time in a slightly different direction…

And I solved it! I was using a scripted command to enter the container that had slightly different options than the standard `./launcher enter app`. One of them (maybe the absence of the `--login` option?) was causing this difference in behavior.

Anyway I am going to make sure I always use `./launcher` going forward, there’s lots of stuff in there. Once again, thanks for your answer.

---

<div class="post-metadata">

### Author: ![pgr](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pgr/32/156783_2.png) [@pgr](https://meta.discourse.org/u/pgr)
#### Post date: [November 12, 2018, 3:15pm UTC](https://meta.discourse.org/t/ruby-mysql2-error-on-migration/63157/9 "2018-11-12T15:15:44Z")

</div>

Another tip that might be useful for anyone else coming into this thread:

Using that command given above with `IMPORT=1` will trigger this part of your `Gemfile`:

```ruby
if ENV["IMPORT"] == "1"
  gem 'mysql2'
  gem 'redcarpet'
  gem 'sqlite3', '~> 1.3.13'
  gem 'ruby-bbcode-to-md', github: 'nlalonde/ruby-bbcode-to-md'
  gem 'reverse_markdown'
  gem 'tiny_tds'
end

```

I ran into problems with the installation of `tiny_tds`, and then with another thing it depends on called `FreeTDS`. After some wasted time on this I realized these are useful only for MS SQL imports, which I won’t be using, so I simply commented out the `tiny_tds` line in my `Gemfile`.

(it would be nice to have a comment in front of each of those packages saying what they are used for, if anyone knows)

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [August 29, 2021, 7:10pm UTC](https://meta.discourse.org/t/ruby-mysql2-error-on-migration/63157/10 "2021-08-29T19:10:01Z")

</div>



---

<div class="post-metadata">

### Author: ![CarlosDaniel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/carlosdaniel/32/273223_2.png) [@CarlosDaniel](https://meta.discourse.org/u/CarlosDaniel)
#### Post date: [September 29, 2022, 1:50pm UTC](https://meta.discourse.org/t/ruby-mysql2-error-on-migration/63157/11 "2022-09-29T13:50:26Z")

</div>

Hi. I’m having the same trouble here when running this script but those commands are not helping… This is the output I get when trying to run the `su discourse -c 'export IMPORT=1; bundle exec ruby script/import_scripts/vanilla_mysql.rb'`:

```plaintext
root@ip-xxx-xx-xx-xx-app:/var/www/discourse# su discourse -c 'export IMPORT=1; bundle exec ruby script/import_scripts/snuffhouse.rb'
You are trying to install in deployment mode after changing
your Gemfile. Run `bundle install` elsewhere and add the
updated Gemfile.lock to version control.

If this is a development machine, remove the /var/www/discourse/Gemfile freeze
by running `bundle config unset deployment`.

The list of sources changed
The dependencies in your gemfile changed

You have added to the Gemfile:
* mysql2
* redcarpet
* sqlite3 (~> 1.3, >= 1.3.13)
* ruby-bbcode-to-md
* reverse_markdown
* tiny_tds
* csv

```

---

<div class="post-metadata">

### Author: ![CarlosDaniel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/carlosdaniel/32/273223_2.png) [@CarlosDaniel](https://meta.discourse.org/u/CarlosDaniel)
#### Post date: [October 24, 2022, 12:38pm UTC](https://meta.discourse.org/t/ruby-mysql2-error-on-migration/63157/12 "2022-10-24T12:38:57Z")

</div>

Hiya, I’ve got this sorted and forgot to reply here. The thing is, I was trying to run a migration script for a forum that uses `mysql` and facing some errors. But I just followed the following steps and it has worked fine:

- Inside of your docker-machine where Discourse is running:

```plaintext
# Enter the project directory
cd /var/www/discourse

# Install MySQL deps
apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y libmariadb-dev

# Install MySQL gems (export = 1)
su discourse -c 'export IMPORT=1; bundle install'

# Run your script (vanilla_mysql in this case)
su discourse -c 'export IMPORT=1; bundle exec ruby script/import_scripts/vanilla_mysql.rb'

```

I’ve just done this in a new machine that I had to pull up and it worked as fine.
