Docker Error 2002 SQLAlchemy [英] Docker Error 2002 SQLAlchemy
问题描述
我正在尝试dockerize它,但是连接到其中一个套接字时遇到了一些麻烦。我已经看了几乎所有关于这个问题,但我仍然得到这个错误。
$ b
sqlalchemy.exc.OperationalError :( _mysql_exceptions.OperationalError)(2002,无法通过套接字连接到本地MySQL服务器/ var / run / mysqld / mysqld .sock'(2)
我目前正在使用Mac和Mac的终端程序。树是:
MainFolder - > .yml,db(文件夹),www(文件夹)
其中db(文件夹)有一个Dockerfile和www(文件夹)具有.py,要求和Dockerfile
以下列出的是我的代码。
docker- compose.yml
版本:'3'
服务:
www:
build: www /。
ports:
- 5000:5000
链接:
- db
depends_on:
- db
db :
build:db /。
volumes:
- ./[database]:/usr/local/mysql
environment:
MYSQL_ROOT_PASSWORD:[password]
Dockerfile(对于www)
FROM python:3 -build
将应用放在容器
COPY。 / opt / www
WORKDIR / opt / www
RUN pip3 install -r requirements.txt
EXPOSE 5000
CMD python index.py
(db)的Dockerfile
#MySQL容器
FROM mysql:5.7.18
EXPOSE 3306
CMD [mysqld]
index.py
app = Flask(__ name__ )
#配置你的MySQL连接
db = SQLAlchemy()
db_uri ='mysql:// root:password @ localhost / [database]'
app。 config ['SQLALCHEMY_DATABASE_URI'] = db_uri
app.config ['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)
$ b $ class Database(db.Model):
$ b versionID = db.Column('versionID',db.Integer,primary_key = True)
versionText = db.Column('versionText',db.String(500),
unique = False)
def __init __(self,versionID,versionText):
self.versionID = versionID
self.versionText = v ersionText
def serialize(self):
return {
'VersionID':self.versionID,
'VersionText':self.versionText
}
@ app.route(/)
def test():
返回jsonify(json_list = [i.serialize for i in
Database.query.all ()])
if __name__ ==__main__:
app.run(host =0.0.0.0,port = 5000)
我的输出结果应该像JSON一样在浏览器中显示:
{ json_list = [
versionID:someID
versionText:someText
]
}
类似的东西。
任何帮助表示赞赏!一直试图解决这个问题!
干杯解决方案根据 documentation ,除非你传递一个环境变量,否则mysql启动时没有数据库可用。
添加这到你的docker-compose文件中的db部分:
$ $ $ $ $ $ $ $ $ $ $ code>
连接字符串应该是:
'mysql:// root:password @ db / name-of-database'
Im currently working on a Flask Application with MySQL / SQLAlchemy.
I am trying to dockerize it but it is having some trouble connecting to one of the sockets. I've looked at nearly all the questions regarding this but I still get this error.
"sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)"
I am currently working on Mac and Mac's Terminal Program.
My Folder Tree is: MainFolder -> .yml, db(folder), www(folder)
where db(folder) has a Dockerfile and www(folder) has the .py, requirements, and Dockerfile
Listed below is my code for this application.
docker-compose.yml
version: '3'
services:
www:
build: www/.
ports:
- 5000:5000
links:
- db
depends_on:
- db
db:
build: db/.
volumes:
- ./[database]:/usr/local/mysql
environment:
MYSQL_ROOT_PASSWORD: [password]
Dockerfile (for www)
FROM python:3-onbuild
# Place app in container
COPY . /opt/www
WORKDIR /opt/www
RUN pip3 install -r requirements.txt
EXPOSE 5000
CMD python index.py
Dockerfile for (db)
# MySQL Container
FROM mysql:5.7.18
EXPOSE 3306
CMD ["mysqld"]
index.py
app = Flask(__name__)
# Configure your MySQL Connection
db = SQLAlchemy()
db_uri = 'mysql://root:password@localhost/[database]'
app.config['SQLALCHEMY_DATABASE_URI'] = db_uri
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)
class Database(db.Model):
versionID = db.Column('versionID', db.Integer, primary_key=True)
versionText = db.Column('versionText', db.String(500),
unique=False)
def __init__(self, versionID, versionText):
self.versionID = versionID
self.versionText = versionText
def serialize(self):
return {
'VersionID' : self.versionID,
'VersionText' : self.versionText
}
@app.route("/")
def test():
return jsonify(json_list=[i.serialize for i in
Database.query.all()])
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
My output should be in the browser in the form of JSON like this:
{ json_list = [ versionID: someID versionText: someText ] }
Something like that.
Any help is appreciated! Been trying to solve this for days! Cheers
According to the documentation, mysql is started with no database that can be used, unless you pass an environment variable.
Add this to the db section in your docker-compose file:
MYSQL_DATABASE: name-of-database
And the connection string should be:
'mysql://root:password@db/name-of-database'
这篇关于Docker Error 2002 SQLAlchemy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!