Mysql迁移到Postgresql
今天把数据库从mssql迁移到了postgresql,主要工作在于数据迁移,使用的pgloader
先把mysql和postgres用docker的--net=host跑起来,或者-p映射端口,这样本地的程序能够连接,使用mysql-client和psql连接测试:
mysql -u root -h 127.0.0.1 -P 3306 -p
psql -U postgres -h localhost -p 5432
这里mysql使用的是127.0.0.1作为hostname而非localhost,否则连不上,因为用localhost时客户端会尝试连接本机的socket,而mysql是跑在容器里的,主机没有这个socket可供连接,postgres没这限制,大约是localhost也用网络连接吧
使用pgload迁移, 注意要把schema改为public,mysql的schema就是database,而postgres中有区别,而连接默认的schema是public
```
pgload.load内容
LOAD DATABASE FROM mysql://root:153226@127.0.0.1:3306/fastdb INTO pgsql://postgres:153226@localhost:5432/fastdb
WITH include drop, create tables, create indexes, workers = 8, concurrency = 1
ALTER SCHEMA \fastdb\ RENAME TO \public\ ; $ pgload pgload.load ```
debian上apt安装的pgloader是3.6.2
pgloader version \"3.6.2\"
compiled with SBCL 2.0.6.debian
使用这个版本会出现如下错误
``` 2023-01-25T16:37:24.025000Z LOG pgloader version \"3.6.2\" 2023-01-25T16:37:24.112000Z LOG Migrating from # 2023-01-25T16:37:24.112000Z LOG Migrating into # 2023-01-25T16:37:24.198000Z ERROR mysql: 76 fell through ECASE expression. Wanted one of (2 3 4 5 6 8 9 10 11 14 15 17 20 21 23 27 28 30 31 32 33 35 41 42 45 46 47 48 49 50 51 52 54 55 56 60 61 62 63 64 65 69 72 77 78 79 82 83 87 90 92 93 94 95 96 97 98 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 254). 2023-01-25T16:37:24.199000Z LOG report summary reset table name errors rows bytes total time
fetch meta data 0 0 0.000s
```
所以从源码编译高一点的版本:
git clone git@github.com:dimitri/pgloader.git
sudo apt-get install sbcl unzip libsqlite3-dev make curl gawk freetds-dev libzip-dev
cd pgloader
make save
再执行成功
``` wwr@vultr:~/pgloader/build/bin$ ./pgloader ~/server_docker_compose/pgload.load 2023-01-25T16:49:59.024000Z LOG pgloader version \"3.6.999791d\" 2023-01-25T16:49:59.088000Z LOG Migrating from # 2023-01-25T16:49:59.088000Z LOG Migrating into # 2023-01-25T16:49:59.968006Z LOG report summary reset table name errors rows bytes total time
fetch meta data 0 52 0.252s
Create Schemas 0 0 0.000s
Create SQL Types 0 0 0.004s
Create tables 0 24 0.056s
Set Table OIDs 0 12 0.004s
public.resource 0 14 2.0 kB 0.060s
public.content 0 11 116.4 kB 0.072s
public.post_tag_relation 0 11 0.0 kB 0.080s public.tag 0 7 0.0 kB 0.084s public.post_tag 0 4 0.0 kB 0.100s public.folder 0 3 0.0 kB 0.072s public.sys_role 0 1 0.0 kB 0.104s public.role_permission 0 0 0.052s public.user_role 0 1 0.0 kB 0.092s public.post_category 0 3 0.0 kB 0.032s public.sys_user 0 1 0.2 kB 0.060s public.sys_permission 0 0 0.060s
COPY Threads Completion 0 8 0.132s Create Indexes 0 26 0.508s Index Build Completion 0 26 0.172s Reset Sequences 0 5 0.084s Primary Keys 0 12 0.008s Create Foreign Keys 0 14 0.012s Create Triggers 0 0 0.000s Install Comments 0 14 0.004s
Total import time ✓ 56 118.7 kB 0.920s
```
迁移的动机,其实早都忘了,不过postgres的内存和磁盘占用都比mysql少了不少,
docker image小了150MB左右,占用数据从200MB减少到64MB,内存从300MB+减少到200MB左右,至于性能,笑死根本没测试,体感慢了