一次外包項目吮炕,需要導入一個150M的sql數(shù)據(jù)庫。至于為什么要導入那么大的數(shù)據(jù)文件土砂,不在這里討論。
硬件
Mysql 使用的是 mariadb疏尿,在Docker上面跑瘟芝。
根據(jù)以往的經(jīng)驗易桃,大文件需要使用命令行導入褥琐。
開始
Liangrongze2019:shante h$ cat shante.sql | docker exec -i 1c450e770130 /usr/bin/mysql -u root --password=123456 shante
ERROR 2006 (HY000) at line 34: MySQL server has gone away
第一次就碰壁!
查看docker容器的日志晤郑,找到錯誤原因敌呈。
Got a packet bigger than 'max_allowed_packet' bytes”
乖乖的找搜索引擎贸宏,原來是sql語句太長導致的錯誤。根據(jù)網(wǎng)上的方案磕洪,修改配置信息吭练。
查看現(xiàn)在的mysql配置
Liangrongze2019:shante h$ docker exec -it 1c450e770130 /bin/bash
root@1c450e770130:/# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.5.4-MariaDB-1:10.5.4+maria~focal mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show VARIABLES like '%max_allowed_packet%';
+--------------------------+------------+
| Variable_name | Value |
+--------------------------+------------+
| max_allowed_packet | 16777216 |
| slave_max_allowed_packet | 1073741824 |
+--------------------------+------------+
2 rows in set (0.005 sec)
還不夠! 修改50M吧析显。
root@1c450e770130:# vim /etc/mysql/mariadb.cnf
#在后面新增一句
max_allowed_packet=50M 加到后面
重啟容器鲫咽!
繼續(xù)執(zhí)行一次
root@1c450e770130:/etc/myscat shante.sql | docker exec -i 1c450e770130 /usr/bin/mysql -u root --password=123456 shante
ERROR 1813 (HY000) at line 95320: Tablespace for table '`shante`.`pay_admin_log_action`' exists. Please DISCARD the tablespace before IMPORT
又一次錯誤!
查找原因谷异,在mysql的data文件夾里面分尸,找到了昨天導入失敗的文件pay_admin_log_action。刪除昨天的文件歹嘹,再來一次箩绍。
Liangrongze2019:shante h$ cat shante.sql | docker exec -i 1c450e770130 /usr/bin/mysql -u root --password=123456 shante
ERROR 1265 (01000) at line 98256: Data truncated for column 'payment_channel_allow' at row 1
sql_mode太嚴格了。
再次修改mysql配置,
vim /etc/mysql/mariadb.conf.d/50-server.cnf
在 [mysqld] 后面增加
sql_mode=ONLY_FULL_GROUP_BY,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER
第三次導入嘗試
Liangrongze2019:shante h$ cat shante.sql | docker exec -i 1c450e770130 /usr/bin/mysql -u root --password=123456 shante
ERROR 1128 (HY000) at line 262319: Function 'ngram' is not defined
我操了尺上! 又有問題2闹搿!
mariadb 不支持ngram! 我怎么知道他們使用了這個索引怎抛。
已經(jīng)花了一個多小時卑吭,累了! 乖乖的安裝mysql吧抽诉!
帶孩子到樓下玩了一圈陨簇,再回來。
使用docker安裝mysql迹淌。
Liangrongze2019:shante h$ docker pull mysql:5.7
跑起來
Liangrongze2019:shante h$ docker run -p 3307:3306 --name mysql -v /Users/h/mydata/mysql/log:/var/log/mysql -v /Users/h/mydata/mysql/data:/var/lib/mysql -v /Users/h/mydata/mysql/conf:/etc/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
36c37854f356344dc560edef26adea45ddcff6f3341c8c9a37dd971372287a77
docker: Error response from daemon: error while creating mount source path '/Users/h/mydata/mysql/conf': mkdir /Users/h/mydata/mysql: operation not permitted.
權限問題河绽,看了一下宿主機的 /Users/h/mydata/mysql 目錄,操唉窃!owner是root耙饰!
乖乖的改回當前用戶。
再來一次
Liangrongze2019:shante h$ cat shante.sql | docker exec -i 98e3862886dd /usr/bin/mysql -u root --password=123456 shante
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2006 (HY000) at line 34: MySQL server has gone away
查找容器的日志纹份,又是之前遇到的錯誤
Got a packet bigger than 'max_allowed_packet' bytes”
這一次駕輕就熟了苟跪!去修改mysql的配置文件!
root@98e3862886dd:/etc/mysql# /usr/bin/mysql --verbose --help|grep -A 1 'Default options'
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf
沒有找到配置文件蔓涧! 操<选!操TE窭!
又費了不少時間茉盏,才發(fā)現(xiàn)掛載文件夾將/etc/mysql覆蓋了鉴未。修改容器啟動配置信息枢冤。
Liangrongze2019:shante h$ docker run -p 3307:3306 --name mysql -v /Users/h/mydata/mysql/log:/var/log/mysql -v /Users/h/mydata/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
de0c759349b8368ce70608c6fcbf4059fd299cb1ad34e938e1498225c63e4d52
Liangrongze2019:shante h$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
de0c759349b8 mysql:5.7 "docker-entrypoint.s…" 24 seconds ago Up 23 seconds 33060/tcp, 0.0.0.0:3307->3306/tcp mysql
53a96c2da8c8 arodax/php7.2-fpm "docker-php-entrypoi…" 9 hours ago Up 4 hours 0.0.0.0:9000->9000/tcp php-fpm
4b44f7d6beef nginx "/docker-entrypoint.…" 12 hours ago Up 9 hours 0.0.0.0:80->80/tcp nginx
root@de0c759349b8:/etc/mysql# vim my.cnf
修改配置,增加一段
max_allowed_packet=50M
重啟容器
這一次沒有問題了吧铜秆!
導入淹真,走你!
Liangrongze2019:shante h$ cat shante.sql | docker exec -i de0c759349b8 /usr/bin/mysql -u root --password=123456 shante
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1292 (22007) at line 256: Incorrect datetime value: '0000-00-00 00:00:00' for column 'order_time' at row 1
你媽逼的连茧! 已經(jīng)要瘋了核蘸! **尼瑪!啸驯!
修改sql_mode 刪除 NO_ZERO_DATE,NO_ZERO_IN_DATE
vim /etc/mysql/mariadb.conf.d/50-server.cnf
在 [mysqld] 后面增加
sql_mode=ONLY_FULL_GROUP_BY,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER
重啟容器 錯誤就會修復值纱。
導入終于成功了!
足足使用了3小時E魈馈虐唠!