flask項目從sqlite3升級的mysql數據庫

flask項目由於需求變動,須要遷移到mysql數據庫。

項目計劃採用docker來管理,採用mysql官網的docker來管理。python

  1. 刪除數據庫目錄。
sudo rm -rf data/
  1. 刪除原日誌目錄。
$ rm -rf log/
  1. 修改docker-compose.yml 文件。 從原來一個項目中複製過來的docker-compose.yml文件,內容以下:
estimate:
  # restart: always
  build: ./app
  ports:
    - "8000:8000"
  links:
    - mysql:mysql
  volumes:
    - ./app:/usr/src/app
  env_file: .env

mysql:
  # restart: always
  image: mysql:latest
  env_file: .env
  ports:
    - "3306:3306"
  volumes:
    - ./data:/var/lib/mysql
    - ./data/my.cnf:/etc/mysql/my.cnf  
  environment:
    - MYSQL_ROOT_PASSWORD=estimate

配置文件改好了,準備構建鏡像:mysql

docker-compose build

看起來一切順利。linux

ERROR: for mysql  oci runtime error: rootfs_linux.go:53: mounting "/var/lib/docker/aufs/mnt/258ac98827bf738a0196693883820ebb3e4801320a2a3542a39076edf9ef1649/etc/mysql/my.cnf" to rootfs "/var/lib/docker/aufs/mnt/258ac98827bf738a0196693883820ebb3e4801320a2a3542a39076edf9ef1649" caused "not a directory"
Traceback (most recent call last):
  File "/usr/local/bin/docker-compose", line 9, in <module>
    load_entry_point('docker-compose==1.7.1', 'console_scripts', 'docker-compose')()
  File "/usr/local/lib/python2.7/dist-packages/compose/cli/main.py", line 63, in main
    log.error(e.msg)
AttributeError: 'ProjectError' object has no attribute 'msg'

看起來狀況不太妙,緣由來配置不能正常拉起來,貌似my.cnf不是一個目錄的緣由,改成映射/etc/mysql試了一下,mysql是初始化成功了,可是容器與映射目錄中均沒有my.cnf文件,暫不知道緣由,先去掉對my.cnf的映射,再次嘗試:sql

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                              NAMES
6112bf03d615        estimate_estimate   "/usr/local/bin/super"   29 seconds ago      Up 28 seconds       5000/tcp, 0.0.0.0:8000->8000/tcp   estimate_estimate_1
5bed08089c16        mysql:latest        "docker-entrypoint.sh"   30 seconds ago      Up 29 seconds       0.0.0.0:3306->3306/tcp             estimate_mysql_1

如今看起來正常多了。 4. 修改mysql的默認字符集爲utf8,修改配置文件,mysql要注意不後字符集不對,後面很麻煩。docker

$ docker exec -it 5bed08089c16 /bin/bash
root@5bed08089c16:/# cd /etc/                                                                                                                                                                                          
root@5bed08089c16:/etc# ll
bash: ll: command not found
root@5bed08089c16:/etc# cd mysql
root@5bed08089c16:/etc/mysql# ll
bash: ll: command not found
root@5bed08089c16:/etc/mysql# ls
conf.d	my.cnf
root@5bed08089c16:/etc/mysql# vim my.cnf 
bash: vim: command not found
root@5bed08089c16:/etc/mysql# vi my.cnf 
bash: vi: command not found
root@5bed08089c16:/etc/mysql#

鬱悶了,容器中沒有vim,連vi也沒有,還好有apt安裝吧。。。。數據庫

root@5bed08089c16:/etc/mysql# apt-get install vim
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package vim
root@5bed08089c16:/etc/mysql#

什麼狀況,貌似,源也不正常。。。flask

apt-get update
apt-get install vim

不是國內源,比較慢,等的時間有點長。。。終於好了。 鏈接mysql查看字符集設置狀況。vim

root@5bed08089c16:/etc/mysql# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.13 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show variables like '%character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | latin1                     |
| character_set_connection | latin1                     |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | latin1                     |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

準備修改字符集配置c#

[client]
port            = 3306
socket          = /var/run/mysqld/mysqld.sock
default-character-set=utf8   //added by janl

[mysqld_safe]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
nice            = 0

[mysqld]
skip-host-cache
skip-name-resolve
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
character_set_server=utf8     // added by janl
init_connect='SET NAMES utf8' //added by janl
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
lc-messages-dir = /usr/share/mysql
explicit_defaults_for_timestamp

好了,讓容器重啓一下,讓配置生效。 再次查看mysql 的字符集配置bash

mysql> show variables like '%character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)

看來字符集都改過來了,一切順利,建立數據庫。

  1. 建立數據庫
mysql> CREATE DATABASE IF NOT EXISTS estimate DEFAULT CHARSET utf8 COLLATE utf8_general_ci;  
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| estimate           |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use estimate;
Database changed
mysql> show tables;
Empty set (0.00 sec)

mysql>

數據庫創建起來了,而後就能夠和數據庫一塊兒愉快的玩耍了。。。。。。。。

相關文章
相關標籤/搜索