flask linux 数据库,Flask Vue.js全栈开发|第20章:Linux云主机部署

1. 部署后端 Flask API

1.1 MySQL

开发过程中使用的 SQLite 数据库不足以支持正式环境中大量用户的读/写请求,由于我们使用了 SQLAlchemy ORM,所以可以很方便的将数据库切换到 MySQL 或 PostgreSQL

(1)安装

这里我将选择安装 MySQL 社区版:

[root@CentOS ~]# wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

[root@CentOS ~]# sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm

[root@CentOS ~]# yum install -y mysql-server

[root@CentOS ~]# systemctl start mysqld

[root@CentOS ~]# systemctl enable mysqld

(2)安全加固

启动 mysqld 服务后,我们还需要对服务器的安全性做一次强化,运行 mysql_secure_installation 命令时,它会提示你设置 root 用户的密码(默认初始时是空密码)、删除 anonymous 用户、删除 test 数据库、不允许 root 用户远程访问等

[root@CentOS ~]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL

SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current

password for the root user. If you've just installed MySQL, and

you haven't set the root password yet, the password will be blank,

so you should just press enter here.

Enter current password for root (enter for none): # 默认是空密码,直接回车即可

OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL

root user without the proper authorisation.

Set root password? [Y/n] y

New password:

Re-enter new password:

Password updated successfully!

Reloading privilege tables..

... Success!

By default, a MySQL installation has an anonymous user, allowing anyone

to log into MySQL without having to have a user account created for

them. This is intended only for testing, and to make the installation

go a bit smoother. You should remove them before moving into a

production environment.

Remove anonymous users? [Y/n] y

... Success!

Normally, root should only be allowed to connect from 'localhost'. This

ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y

... Success!

By default, MySQL comes with a database named 'test' that anyone can

access. This is also intended only for testing, and should be removed

before moving into a production environment.

Remove test database and access to it? [Y/n] y

- Dropping test database...

ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist

... Failed! Not critical, keep moving...

- Removing privileges on test database...

... Success!

Reloading the privilege tables will ensure that all changes made so far

will take effect immediately.

Reload privilege tables now? [Y/n] y

... Success!

All done! If you've completed all of the above steps, your MySQL

installation should now be secure.

Thanks for using MySQL!

Cleaning up...

(3)创建 Flask 应用需要的数据库

使用刚更改密码后的 root 用户登录 MySQL:

[root@CentOS ~]# mysql -u root -p

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 15

Server version: 5.6.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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>

假设我们将创建名为 madblog 的数据库,和用户名为 testuser 的用户,密码是 password:

mysql> create database madblog character set utf8 collate utf8_general_ci;

Query OK, 1 row affected (0.00 sec)

mysql> grant all on madblog.* to 'testuser' identified by 'password';

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

mysql> quit;

Bye

1.2 Redis

我们后端应用会使用 RQ 任务队列,它需要 Redis:

[root@CentOS ~]# yum install -y epel-release

[root@CentOS ~]# yum install -y redis

[root@CentOS ~]# systemctl start redis

[root@CentOS ~]# systemctl enable redis

1.3 Elasticsearch

(1)安装

我们后端应用会提供全文检索功能,需要 Elasticsearch 7.0:

# 0. Need Java 8++

[root@CentOS ~]# yum -y install java-1.8.0-openjdk

# 1. Download and install the public signing key

[root@CentOS ~]# rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch

# 2. Create yum repository

[root@CentOS ~]# vim /etc/yum.repos.d/elasticsearch.repo

内容如下:

[elasticsearch-7.x]

name=Elasticsearch repository for 7.x packages

baseurl=https://artifacts.elastic.co/packages/7.x/yum

gpgcheck=1

gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch

enabled=1

autorefresh=1

type=rpm-md

# 3. Install Elasticsearch

[root@CentOS ~]# yum -y install elasticsearch

[root@CentOS ~]# systemctl start elasticsearch.service

[root@CentOS ~]# systemctl enable elasticsearch.service

(2)安装中文分词 ik

由于博文基本上是中文字符,使用 Elasticsearch 默认的词法分析器效果不好,所以需要安装 elasticsearch-analysis-ik

首先确认你的 Elasticsearch

你可能感兴趣的:(flask,linux,数据库)