Projekt

Allgemein

Profil

Owncloud server migrate SQLite3 to MySQL » Historie » Version 2

Jeremias Keihsler, 13.01.2017 18:24

1 1 Jeremias Keihsler
h1. Migrate owncloud 5 from SQLite3 to MySQL
2
3
Maybe at some point you realize, that it was not a good idea to have owncloud initially set up with the SQLite-Backend and want to change to MySQL.
4
5
h2. Preliminary Note
6
7
this is basically taken from http://fabianpeter.de/cloud/owncloud-migrating-from-sqlite-to-mysql/ 
8
9
and extended with information from
10
* http://trac.edgewall.org/wiki/SqLiteToMySql
11
* http://pario.no/2008/01/27/mysql-create-utf8-database/
12
* http://www.redmine.org/boards/2/topics/12793
13
14
h2. Migration
15
16
h3. Install MySQL and necessary php-modules
17
18
<pre><code class="bash">
19
yum install mysql mysql-server php-mysql
20
</code></pre>
21
22
h3. Setup MySQL
23
24
We need to make sure that @MySQL@ is started up automatically at system start and need to set a root password for @MySQL@
25
<pre><code class="bash">
26
chkconfig mysqld on
27
/etc/init.d/mysqld start
28
/usr/bin/mysql_secure_installation
29
</code></pre>
30
31
h3. Create owncloud Database
32
33 2 Jeremias Keihsler
<pre><code class="sql">
34 1 Jeremias Keihsler
mysql -p
35
CREATE DATABASE owncloud CHARACTER SET utf8 COLLATE utf8_general_ci;
36
GRANT ALL PRIVILEGES ON owncloud.* TO owncloud IDENTIFIED BY "pasword";
37
flush privileges;
38
exit
39
</code></pre>
40
41
h3. Dump SQLite3-Database
42
43
<pre><code class="bash">
44
sqlite3 /var/www/html/owncloud/data/owncloud.db .dump > /tmp/dump.sql
45
</code></pre>
46
47
h3. Convert SQLite3 to MySQL
48
49
Since MySQL uses a pretty special comment/escape format and SQLite uses some commands MySQL doesn’t understand, the SQL output has to be converted. Since my database is quite big and I don’t want to repeat the work others did before me, a little Google magic came up with a python script that does the converter job.
50
51
<pre><code class="python">
52
#! /usr/bin/env python
53
54
import sys
55
56
def main():
57
    print "SET sql_mode='NO_BACKSLASH_ESCAPES';"
58
    lines = sys.stdin.read().splitlines()
59
    for line in lines:
60
        processLine(line)
61
62
def processLine(line):
63
    if (
64
        line.startswith("PRAGMA") or 
65
        line.startswith("BEGIN TRANSACTION;") or
66
        line.startswith("COMMIT;") or
67
        line.startswith("DELETE FROM sqlite_sequence;") or
68
        line.startswith("INSERT INTO \"sqlite_sequence\"")
69
       ):
70
        return
71
    line = line.replace("AUTOINCREMENT", "AUTO_INCREMENT")
72
    line = line.replace("DEFAULT 't'", "DEFAULT '1'")
73
    line = line.replace("DEFAULT 'f'", "DEFAULT '0'")
74
    line = line.replace(",'t'", ",'1'")
75
    line = line.replace(",'f'", ",'0'")
76
    in_string = False
77
    newLine = @
78
    for c in line:
79
        if not in_string:
80
            if c == "'":
81
                in_string = True
82
            elif c == '"':
83
                newLine = newLine + '`'
84
                continue
85
        elif c == "'":
86
            in_string = False
87
        newLine = newLine + c
88
    print newLine
89
90
if __name__ == "__main__":
91
    main()
92
</code></pre>
93
94
download above file to your @/home@, make it executable and perform the conversion
95
<pre><code class="bash">
96
chmod a+x /~/sqlite3-to-mysql.py
97
cat /tmp/dump.sql | python sqlite3-to-mysql.py > /tmp/owncloud.sql
98
</code></pre>
99
100
h3. Import the dump
101
102
One thing to note is that SQLite uses utf8 as default. If you have your server configure with encoding other than utf8 and you can't change that, you are going to get your data bad encoded.
103
104
Despite the fact that you may probably already set your MySQL database to use utf8 as a default character set, you could have problems when you import data. It's very important to keep in mind that your connection encoding counts. So be sure that whatever software you use as client you set your charset as utf8. As an example for the console client: 
105
<pre><code class="bash">
106
mysql -u owncloud -p owncloud --default-character-set=utf8 < /tmp/owncloud.sql
107
</code></pre>
108
109
h3. Change owncloud-config
110
111
While most people seem to change the “installed” key in the config array (@config/config.php@) from **true** to **false**, I found it simpler to just add the correct database values.
112 2 Jeremias Keihsler
<pre>
113 1 Jeremias Keihsler
...
114
‘dbtype’ => ‘mysql’,
115
‘dbname’ => ‘owncloud’,
116
‘dbuser’ => ‘owncloud’,
117
‘dbpassword’ => ‘password’,
118
‘dbhost’ => ’127.0.0.1',
119
‘dbtableprefix’ => ‘oc_’,
120
...
121
</pre>