Skip to content

Instantly share code, notes, and snippets.

@RyosukeKamei
Last active September 4, 2016 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RyosukeKamei/e7bf71cdd7e91ef0c3a28749b0bad5e4 to your computer and use it in GitHub Desktop.
Save RyosukeKamei/e7bf71cdd7e91ef0c3a28749b0bad5e4 to your computer and use it in GitHub Desktop.
RaspberryPi3(ラズパイ)にDockerでPython+bottle+MySQL環境構築する!【試行錯誤編】 ref: http://qiita.com/RyosukeKamei/items/5905240b4807fab00bc6
# git clone https://github.com/mysql/mysql-connector-python.git
<html>
<body>
はろーわーるど
</body>
</html>
# ./configure && make && make install
mysql_server # mysql -u{ユーザ名} -p
(パスワードを入力)
# apt-get install -y python3-pip
# pip3 install --upgrade pip
# tar xvf Python-3.5.2.tgz
# wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz
# apt-get install -y build-essential libncursesw5-dev libgdbm-dev libc6-dev zlib1g-dev libsqlite3-dev tk-dev libssl-dev openssl libbz2-dev libreadline-dev
sudo docker run -it hypriot/rpi-python /bin/bash
from bottle import route, run
# MySQLドライバはmysql.connector
import mysql.connector
# hostのIPアドレスは、$ docker inspect {データベースのコンテナ名}で調べる
# MySQLのユーザやパスワード、データベースはdocker-compose.ymlで設定したもの
connector = mysql.connector.connect(
user='python',
password='python',
host='178.10.0.3', # データベースサーバのIPアドレス
database='sample' # 作成したデータベース
)
cursor = connector.cursor()
cursor.execute("select * from users")
disp = ""
for row in cursor.fetchall():
disp = "ID:" + str(row[0]) + " 名前:" + row[1]
cursor.close
connector.close
@route('/hello')
def hello():
return "DBから取得 "+disp
run(host='0.0.0.0', port=8080, debug=True, reloader=True)
pi@raspberrypi $ sudo docker exec -it {アプリケーションサーバのコンテナ名}
# /usr/local/bin/python3 /home/bottle/server.py
# groupadd web
# useradd -d /home/bottle -m bottle
$ python3 -m http.server 8080
Serving HTTP on 0.0.0.0 port 8080 ...
mysql>
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
mysql>
INSERT INTO `users` (`id`, `name`) VALUES (1, 'pyhons');
mysql_server # (Contrl + p, Control + q)
pi@raspberrypi $ sudo docker exec -it {データベースサーバのコンテナ名}
# python3 ./setup.py build
# python3 ./setup.py install
# apt-get install -y vim
# apt-get install -y sudo
# apt-get install -y wget
# apt-get install -y git
# cd mysql-connector-python
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment