# Mybb.rb 不导入任何内容

**URL:** <https://meta.discourse.org/t/mybb-rb-imports-nothing/42070>\
**Category:** Migration\
**Tags:** mybb\
**Created:** [2016年四月5日 14:50 UTC](https://meta.discourse.org/t/mybb-rb-imports-nothing/42070 "2016-04-05T14:50:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![precessor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/precessor/32/120664_2.png) [@precessor](https://meta.discourse.org/u/precessor)\
**Post date:** [2016年四月5日 14:50 UTC](https://meta.discourse.org/t/mybb-rb-imports-nothing/42070/1 "2016-04-05T14:50:25Z")

</div>

I’m running the mybb.rb script as [described](https://meta.discourse.org/t/migrating-from-mybb/25563/2) by @elberet and the script is running and returns successfully, but no changes result. The script contains the correct remote host name, db name, pw, etc. The remote server is configured to allow remote mysql from the IP of the current Discourse install. I’m not sure why nothing is imported. Can anyone shed any light?

 ![](https://global.discourse-cdn.com/meta/original/3X/2/f/2ffbc2b8cc3709f7afc67ecec50e3876472fc43c.png)(I believe the three users mentioned here are the existing users already in the Discourse to which I am trying to import)

---

<div class="post-metadata">

**Author:** ![precessor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/precessor/32/120664_2.png) [@precessor](https://meta.discourse.org/u/precessor)\
**Post date:** [2016年四月6日 14:59 UTC](https://meta.discourse.org/t/mybb-rb-imports-nothing/42070/2 "2016-04-06T14:59:18Z")

</div>

@dandv I followed your list of steps here

> [@Migrating from mybb](https://meta.discourse.org/t/migrating-from-mybb/25563/5):
>
> @elberet, thanks for your help so far. I went for the second option - connecting from the Discourse machine to the MySQL server of the running forum (set to maintenance mode) after enabling port 3306 in the firewall and setting bind-address = 0.0.0.0 in /etc/mysql/my.cnf followed by a sudo service mysql restart. Here’s what I’ve done based on your advice: cd /var/discourse ./launcher enter app apt-get update apt-get install libmysqlclient-dev mc -y gem install mysql2 # mysql2-0.3.18 got instal…

(except for the edit to my.cnf on the remote server).

You seem to have had some experience with a mybb db import, do you have any idea why the mybb.rb script doesn’t pick up the data from the remote database?  
Thanks for any insight!

---

<div class="post-metadata">

**Author:** ![precessor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/precessor/32/120664_2.png) [@precessor](https://meta.discourse.org/u/precessor)\
**Post date:** [2016年四月6日 22:30 UTC](https://meta.discourse.org/t/mybb-rb-imports-nothing/42070/3 "2016-04-06T22:30:36Z")

</div>

Is there a log of the importer’s actions which I can check?  
I’m stuck because the importer returns successfully, but does not result in import of anything. Thanks.

---

<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:** [2016年四月6日 22:42 UTC](https://meta.discourse.org/t/mybb-rb-imports-nothing/42070/4 "2016-04-06T22:42:08Z")

</div>

> [@precessor](#):
>
> I believe the three users mentioned here are the existing users already in the Discourse to which I am trying to import

Do you have more than 3 users in your myBB forum? Then you’ll need to figure out why [these queries](https://github.com/discourse/discourse/blob/master/script/import_scripts/mybb.rb#L34-L47) return only 3 users.

Regarding the topics and posts… You’ll have to [debug those queries](https://github.com/discourse/discourse/blob/master/script/import_scripts/mybb.rb#L83-L101) too. Do they return all the posts from your forum?

---

<div class="post-metadata">

**Author:** ![precessor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/precessor/32/120664_2.png) [@precessor](https://meta.discourse.org/u/precessor)\
**Post date:** [2016年四月7日 10:31 UTC](https://meta.discourse.org/t/mybb-rb-imports-nothing/42070/5 "2016-04-07T10:31:48Z")

</div>

I have hundreds of users and thousands of posts on the remote forum. I have presumed that the “creating users 3 / 3” is somehow related to the Discourse forum, where I happen to have three users set up. But the script is definitely set up to contact a remote host:

 ![](https://global.discourse-cdn.com/meta/original/3X/2/f/2fa3960ce8dec39e69c95f3839db5e43eccd1228.png)

The table prefix is correct; from my remote db phpMyAdmin:

 ![](https://global.discourse-cdn.com/meta/original/3X/9/6/9603bf3a50439c4ab5c5f075571f3b3c37d56c61.png)

After the mybb.rb script returns, there are no new users in Discourse, and no changes to the existing three users, no new posts, nothing!

If I change any of the connection details, the script throws an error. (host, username, pw)  
So I think it’s connecting ok.

---

<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:** [2016年四月7日 10:49 UTC](https://meta.discourse.org/t/mybb-rb-imports-nothing/42070/6 "2016-04-07T10:49:07Z")

</div>

Did you try to execute the queries with phpMyAdmin to which I posted links in my previous post?  
Here they are with the right table prefix for your convenience:

```sql
SELECT count(*) count
 FROM mybb_users u
 JOIN mybb_usergroups g ON g.gid = u.usergroup
WHERE g.title != 'Banned';

SELECT uid id, email email, username, regdate, g.title `group`
  FROM mybb_users u
  JOIN mybb_usergroups g ON g.gid = u.usergroup
 WHERE g.title != 'Banned'
 ORDER BY u.uid ASC;

SELECT count(*) count
  FROM mybb_posts;

SELECT p.pid id,
       p.tid topic_id,
       t.fid category_id,
       t.subject title,
       t.firstpost first_post_id,
       p.uid user_id,
       p.message raw,
       p.dateline post_time
  FROM mybb_posts p,
       mybb_threads t
 WHERE p.tid = t.tid
ORDER BY p.dateline;

```

Do they work and return the expected amount of rows? If they don’t, try tweaking them until they work. 😉

---

<div class="post-metadata">

**Author:** ![precessor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/precessor/32/120664_2.png) [@precessor](https://meta.discourse.org/u/precessor)\
**Post date:** [2016年四月7日 11:33 UTC](https://meta.discourse.org/t/mybb-rb-imports-nothing/42070/7 "2016-04-07T11:33:10Z")

</div>

Ah I see - in phpMyAdmin :facepalm: 😄  
Thanks for editing the queries for testing.  
In phpMyAdmin where the mybb forum resides, those queries work just fine! I get my correct count of users, the user list, post count, and posts list.  
So the queries look ok.

And, if I add `puts '', total_count` into mybb.rb, after that mysql query is executed, I see the correct result. So the query is working from remote as well.

 ![](https://global.discourse-cdn.com/meta/original/3X/3/b/3bf919ac0bcd1026d9cd87f4c4d5fd9b6f9f33c7.png)

---

<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:** [2016年四月7日 11:45 UTC](https://meta.discourse.org/t/mybb-rb-imports-nothing/42070/8 "2016-04-07T11:45:03Z")

</div>

It’s hard to tell without debugging the import script…  
The only thing I can think of is, that all record are skipped during the import. Try removing all lines which start with `next if all_records_exist?` from the import script.

---

<div class="post-metadata">

**Author:** ![precessor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/precessor/32/120664_2.png) [@precessor](https://meta.discourse.org/u/precessor)\
**Post date:** [2016年四月7日 13:45 UTC](https://meta.discourse.org/t/mybb-rb-imports-nothing/42070/9 "2016-04-07T13:45:41Z")

</div>

I commented out the `next if all_records_exist?` as suggested, and: It’s working - thanks!

Interestingly, the user count starts going up:

 ![](https://global.discourse-cdn.com/meta/original/3X/f/8/f839e1b54277407d2e1da2c7d339f3d6f0aad7a2.png)

but when it reaches it’s total, it pings back to “3 / 3”:

 ![](https://global.discourse-cdn.com/meta/original/3X/2/5/25a3a99dbcf5f27de7c680871953107ee4fb5004.png)

Perhaps this is part of the issue. No matter, I suppose.

However, when the posts-total is reached (or the batch-size total, for a large batch size), the script prematurely aborts with “MySQL server has gone away” which sounds very sad 😉

So we’re making progress. But - come back, MySQL server 😣 I want the script to finish! It seems there are a lot of reasons this could happen, according the _the Internet_, but I fixed this issue by simply dramatically reducing the BATCH\_SIZE in myrb.bb (to 50). Then the remaining parts of the script ran as well.

I’m there! I imported my mybb db! After 3 days of trying! 😊 🙄  
@gerhard, thanks SO much for your tips.

---

<div class="post-metadata">

**Author:** ![precessor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/precessor/32/120664_2.png) [@precessor](https://meta.discourse.org/u/precessor)\
**Post date:** [2016年四月7日 14:57 UTC](https://meta.discourse.org/t/mybb-rb-imports-nothing/42070/10 "2016-04-07T14:57:38Z")

</div>

For others wishing to import a mybb MySQL database, here’s what worked for me, from start to finish. My mybb was hosted on a server with cpanel.

I used a live fresh-install of Discourse on DigitalOcean. It is advised [elsewhere](https://meta.discourse.org/t/migrating-from-mybb/25563/2) to import into a local developer Discourse and then backup that Discourse and restore into the live one. However that is rather involved if you’re not a regular Github and Vagrant user (see [this](https://github.com/discourse/discourse/blob/master/docs/VAGRANT.md) and [this](http://blog.discourse.org/2013/04/discourse-as-your-first-rails-app/)). So if the live Discourse is new, why not import right into it. If everything breaks, I can start over and have Discourse back up in [30 mins](https://github.com/discourse/discourse/blob/master/docs/INSTALL-cloud.md).

Start by performing a new Discourse backup through admin pages, and download it.

Then you need to allow remote MySQL connections from the new (Discourse) server.  
I used cpanel where my mybb forum is hosted, and clicked on “Remote MySQL” and entered the Discourse server IP there.

Then collect the mybb database connection details. In cpanel you can see these under “MySQL databases”. Note the database name, the user, and look up in your records the password for that user (you did make a record of that, right?!) For the IP of the server where your mybb forum resides, you may be able to just do a DNS lookup on the domain name. But my hosting provider advised using a different IP for the server where the shared hosting resides. (Both may work, I am not sure.) If your table prefix is not “mybb\_”, you’ll need to edit that too (you can check in phpMyAdmin from cpanel).

Now, ssh to your Discourse install. I did this as root, with PuTTY.  
Then,

`cd /var/discourse/ ./launcher enter app apt-get update apt-get install libmysqlclient-dev nano gem install mysql2 su - discourse cd /var/www/discourse/script/import_scripts bundle install`

`nano mybb.rb`  
…and edit the connection details (MYBB\_DB, host, username, password). I also needed to comment out the two lines beginning `next if`, and reduce the BATCH\_SIZE to 50 (see above in this topic for more info).  
To save the file in nano, ctrlO. To exit nano, ctrlX.

Now go back to your mybb admin page and put the board into offline mode, in the Configuration tab.

Now back in your ssh window to your Discourse install,

`RAILS_ENV=production ruby mybb.rb`

At this point, for me, the script executed successfully. Check your Discourse install - is everything there? (Don’t be fooled by the Dashboard stats, [they may not update for a while](https://meta.discourse.org/t/after-update-it-takes-a-while-for-admin-dashboard-stats-to-update/31160/12))

In the online Discourse admin UI, perform another backup, and download it. There now may be some manual cleanup of posts required (links, attachments etc).

Once the import is complete,  
`exit` out of the discourse login  
`exit` out of the Rails app

If you now  
`./launcher rebuild app`  
the libmysqlclient-dev, nano, and mysql2 gem installations should be removed, as well as your edits to mybb.rb.

Thanks to @elberet, @dandv and @gerhard for their posts on this task

---

<div class="post-metadata">

**Author:** ![precessor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/precessor/32/120664_2.png) [@precessor](https://meta.discourse.org/u/precessor)\
**Post date:** [2016年四月11日 16:43 UTC](https://meta.discourse.org/t/mybb-rb-imports-nothing/42070/11 "2016-04-11T16:43:37Z")

</div>

Something else I edited, might be useful to others was the SQL queries.

These changes to the mybb.rb script achieve the following:

- do not import any users in the “Awaiting Activation” group
- do not import users who have (1) never posted and (2) haven’t visited the forum for 365 days or longer.

```sql
total_count = mysql_query("SELECT count(*) count
    FROM #{TABLE_PREFIX}users u
    JOIN #{TABLE_PREFIX}usergroups g ON g.gid = u.usergroup
    WHERE g.title != 'Banned'
       AND g.title != 'Awaiting Activation'
       AND !((postnum < 1) AND (DATEDIFF(now(), FROM_UNIXTIME(lastactive)) > 365) )
    ;").first['count']

batches(BATCH_SIZE) do |offset|
    results = mysql_query(
      "SELECT uid id, email email, username, regdate, g.title `group`
       FROM #{TABLE_PREFIX}users u
       JOIN #{TABLE_PREFIX}usergroups g ON g.gid = u.usergroup
       WHERE g.title != 'Banned'
          AND g.title != 'Awaiting Activation'
          AND !((postnum < 1) AND (DATEDIFF(now(), FROM_UNIXTIME(lastactive)) > 365) )
          ORDER BY u.uid ASC
          LIMIT #{BATCH_SIZE}
          OFFSET #{offset};")

```

---

<div class="post-metadata">

**Author:** ![bekircem](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bekircem/32/44582_2.png) [@bekircem](https://meta.discourse.org/u/bekircem)\
**Post date:** [2017年七月30日 22:41 UTC](https://meta.discourse.org/t/mybb-rb-imports-nothing/42070/12 "2017-07-30T22:41:56Z")

</div>

> [@precessor](#):
>
> RAILS\_ENV=production ruby mybb.rb

What would you recommend in this case?

```
loading existing groups...
loading existing users...
loading existing categories...
loading existing posts...
loading existing topics...
/usr/local/lib/ruby/gems/2.3.0/gems/mysql2-0.4.8/lib/mysql2/client.rb:89:in `connect': Access denied for user 'root'@'localhost' (using password: NO) (Mysql2::Error)
        from /usr/local/lib/ruby/gems/2.3.0/gems/mysql2-0.4.8/lib/mysql2/client.rb:89:in `initialize'
        from mybb.rb:31:in `new'
        from mybb.rb:31:in `initialize'
        from mybb.rb:304:in `new'
        from mybb.rb:304:in `<main>'

```

---

<div class="post-metadata">

**Author:** ![mpalmer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mpalmer/32/45740_2.png) [@mpalmer](https://meta.discourse.org/u/mpalmer)\
**Post date:** [2017年七月30日 22:44 UTC](https://meta.discourse.org/t/mybb-rb-imports-nothing/42070/13 "2017-07-30T22:44:25Z")

</div>

Set a password for your MySQL connection, most likely.

---

<div class="post-metadata">

**Author:** ![bekircem](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bekircem/32/44582_2.png) [@bekircem](https://meta.discourse.org/u/bekircem)\
**Post date:** [2017年七月30日 23:03 UTC](https://meta.discourse.org/t/mybb-rb-imports-nothing/42070/14 "2017-07-30T23:03:15Z")

</div>

I set it already. ☹

---

<div class="post-metadata">

**Author:** ![mpalmer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mpalmer/32/45740_2.png) [@mpalmer](https://meta.discourse.org/u/mpalmer)\
**Post date:** [2017年七月30日 23:05 UTC](https://meta.discourse.org/t/mybb-rb-imports-nothing/42070/15 "2017-07-30T23:05:16Z")

</div>

And yet…

> `Access denied for user 'root'@'localhost' (using password: NO)`

---

<div class="post-metadata">

**Author:** ![bekircem](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bekircem/32/44582_2.png) [@bekircem](https://meta.discourse.org/u/bekircem)\
**Post date:** [2017年七月30日 23:08 UTC](https://meta.discourse.org/t/mybb-rb-imports-nothing/42070/16 "2017-07-30T23:08:19Z")

</div>

I change my mysql root password with the new. That command:

`mysqladmin -u root -p'oldpass' password 'newpass'`

And tried again.

`RAILS_ENV=production ruby mybb.rb`

Output:

```
discourse@discourse-gameofthronestr-app:/var/www/discourse/script/import_scripts$ RAILS_ENV=production ruby mybb.rb
loading existing groups...
loading existing users...
loading existing categories...
loading existing posts...
loading existing topics...
/usr/local/lib/ruby/gems/2.3.0/gems/mysql2-0.4.8/lib/mysql2/client.rb:89:in `connect': Access denied for user 'root'@'localhost' (using password: YES) (Mysql2::Error)
        from /usr/local/lib/ruby/gems/2.3.0/gems/mysql2-0.4.8/lib/mysql2/client.rb:89:in `initialize'
        from mybb.rb:31:in `new'
        from mybb.rb:31:in `initialize'
        from mybb.rb:304:in `new'
        from mybb.rb:304:in `<main>'

```

---

<div class="post-metadata">

**Author:** ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)\
**Post date:** [2017年七月31日 05:32 UTC](https://meta.discourse.org/t/mybb-rb-imports-nothing/42070/17 "2017-07-31T05:32:46Z")

</div>

You have to provide your login/password as environment variables when running the import script.

---

<div class="post-metadata">

**Author:** ![bekircem](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bekircem/32/44582_2.png) [@bekircem](https://meta.discourse.org/u/bekircem)\
**Post date:** [2017年七月31日 14:56 UTC](https://meta.discourse.org/t/mybb-rb-imports-nothing/42070/18 "2017-07-31T14:56:15Z")

</div>

I set db and password variables. Except this there is no variable in Mybb.rb. How can I provide variables?

---

<div class="post-metadata">

**Author:** ![bekircem](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bekircem/32/44582_2.png) [@bekircem](https://meta.discourse.org/u/bekircem)\
**Post date:** [2017年七月31日 18:34 UTC](https://meta.discourse.org/t/mybb-rb-imports-nothing/42070/19 "2017-07-31T18:34:29Z")

</div>

> [@precessor](#):
>
> RAILS\_ENV=production ruby mybb.rb

I reinstalled it.

when i use that command: `RAILS_ENV=production ruby mybb.rb`

Output:

 ![error](https://global.discourse-cdn.com/meta/original/3X/2/f/2f90357383ade2c3f805982caafd82e5f85c4d49.png)

mybb.rb screenshot:

 ![mybbrb](https://global.discourse-cdn.com/meta/original/3X/8/6/86917a14634fcc1b0a1dabc94dd820e2d54e5116.png)

Any ideas?

---

<div class="post-metadata">

**Author:** ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)\
**Post date:** [2017年七月31日 18:41 UTC](https://meta.discourse.org/t/mybb-rb-imports-nothing/42070/20 "2017-07-31T18:41:31Z")

</div>

If you installed the development environment, don’t include production when you start the script.

Oh, but the problem is that it can’t connect to the MySQL database. It looks like it’s not even getting connected. Is it running and the local machine?

[下一頁](https://meta.discourse.org/t/mybb-rb-imports-nothing/42070.md?page=2)
