2021最新ubuntu20.04LTS下MysqL安装及flask访问
-
安装
- apt安装
sudo apt-get update sudo apt-get install -y MysqL
sudo dpkg -i MysqL-apt-config*
-
设置数据库安全性,根据需求填写,注意对于MysqL8来说只有2类密码(数字,大写字母,小写字母,下划线,大于8位)才能通过Python/java,以非sudo权限用户访问到数据库
sudo MysqL_secure_installation
- 下载MysqL官方可视化管理工具MysqL-workbench(类似于sqlserver的ssms)
访问MysqL-workbench的官网,选择ubuntu-linux,选择ubuntu-linux 20.04,下载两个deb文件,其中带-dbgsym的文件的依赖是不带的,需要先安装MysqL-workbench-community
sudo dpkg -i MysqL-workbench-community_*
再安装MysqL-workbench-community-dbgsym
sudo dpkg -i MysqL-workbench-community-dbgsym*
- 如果出现Error 1045(28000):Access denied for user ‘root‘@‘localhost‘ (using password: YES),需要修改密码为2类密码(数字,大写字母,小写字母,下划线,大于8位)
sudo MysqL
#下面进入MysqL>
MysqL> alter user 'root'@'localhost' identified with MysqL_native_password by '新密码';
MysqL> exit;
#退出MysqL后重启服务
sudo service MysqL restart
- 执行你的sql语句
- 基于conda命令配置flask(由于pip安装可能会产生依赖冲突,推荐使用conda来规避。出于常规的前后端分离产品,推荐安装以下的依赖,其中name区域是自己的环境名称),conda切换清华源的操作可以参考清华源提供的使用说明
conda create -n name flask flask-login flask-sqlalchemy flask-wtf MysqLclient pyMysqL python=3.8
#或者在base环境或者需要使用的环境下执行
conda install -y flask flask-login flask-sqlalchemy flask-wtf MysqLclient pyMysqL
- 在项目目录下创建app.py文件,在项目目录下创建templates文件夹,里面放入html、css、js等文件,通过下面的代码即可实现在index.html文件插入超链接的结果,并放到服务器上(根据需要修改route和文件名即可,想要动态插入网页可以使用glob在index.html里传参)
from flask import Flask,render_template,send_from_directory
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.htm')
@app.route('/<path:filename>')
def custom_static(filename):
return send_from_directory(directory='templates/',filename=filename)
app.run()
- 在项目目录下的终端运行即可在本地搭建服务器
flask run
<!DOCTYPE html>
<html>
<head>
<Meta charset="UTF-8">
<title>test</title>
<Meta name="description" content="你的作品">
<Meta name="author" content="你的名字">
</head>
<body>
{% for row in results %}
{% for column in dct[i].items() %}
<p style="text-align: left;">
{{column[1]}}:{{column[2]}<br>
</p>
{% endfor %}
{% endfor %}
</body>
</html>
@app.route('/test.html')
def sql():
import MysqL
db = pyMysqL.connect(host="127.0.0.1", user="root",password="改成你的密码",database="改成你的数据库",charset="utf8")
cursor = db.cursor()
sql="你的数据库查询语句"
cursor.execute(sql)
results = cursor.fetchall()
db.close()
return render_template('test.html',results=results)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。