一、将数据导入到数据库中
登录Mysql
[root@localhost ~]# mysql -u root -p
Enter password:
添加新的用户
MariaDB [mysql]> create user 'yangzhaoming'@'%' identified by '123456';
Query OK, 0 rows affected (0.000 sec)
- 用户创建完成后,刷新授权
MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.000 sec)
创建utf-8编码的数据库
MariaDB [(none)]> CREATE DATABASE`test` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.000 sec)
MariaDB [(none)]> use test2 ;
Database changed
将改用户yangzhaoming赋权给数据库test2
- 允许外网IP访问数据库test2,本命令包含上面的命令,是所有的IP都可以访问该数据库
MariaDB [mysql]> grant all privileges on `test2`.* to 'yangzhaoming'@'%' identified by '123456' with grant option;
Query OK, 0 rows affected (0.000 sec)
MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.000 sec)
切换yangzhaoming用户
[root@localhost ~]# mysql -uyangzhaoming -p123456
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| test2 |
+--------------------+
创建数据表
MariaDB [test2]> create table movie250 (name_url text,name varchar(40),Ename text,img_url text,score float,director text,lead text);
Query OK, 0 rows affected (0.004 sec)
MariaDB [test2]> select * from movie250;
Empty set (0.001 sec)
导入数据消息
MariaDB [test2]> source /root/test/movie250.sql
.......
Query OK, 1 row affected (0.003 sec)
Query OK, 1 row affected (0.001 sec)
查询MySQL数据库是否有数据
MariaDB [test2]> select count(*) from movie250;
+----------+
| count(*) |
+----------+
| 1323 |
+----------+
1 row in set (0.001 sec)
二、将项目打包发送服务器并解压
[root@CentOSA ~]# cd /usr/share/nginx/html/
[root@CentOSA html]# unzip movie_flask.zip
[root@CentOSA movie_flask]# ls
app.py flask_bootstrap layui __pycache__ static templates venv
[root@CentOSA movie_flask]# mv * /usr/share/nginx/html/
[root@CentOSA movie_flask]# cd ..
[root@CentOSA html]# ls
app.py flask_bootstrap layui movie_flask nginx-logo.png poweredby.png __pycache__ static templates venv
[root@CentOSA html]# rm -rf movie_flask/
[root@CentOSA html]# ls
app.py flask_bootstrap layui nginx-logo.png poweredby.png __pycache__ static templates venv
三、安装项目所需要的依赖
安装flask框架
[root@CentOSA html]# pip3 install flask
安装pymysql包
[root@CentOSA html]# pip3 install pymysql
运行flask项目
[root@CentOSA html]# python3 app.py
* Serving Flask app 'app' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
[root@CentOSA ~]# netstat -antup |grep 5000
tcp 0 0 127.0.0.1:5000 0.0.0.0:* LISTEN 2808/python3
通过nginx服务对外提供访问
[root@CentOSA ~]# vi /etc/nginx/nginx.conf
location / {
proxy_pass http://127.0.0.1:5000;
}
[root@CentOSA nginx]# systemctl restart nginx
[root@CentOSA nginx]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2021-07-26 17:13:15 CST; 14s ago
Process: 2941 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 2938 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 2936 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 2943 (nginx)
Tasks: 2 (limit: 11155)
Memory: 4.1M
CGroup: /system.slice/nginx.service
├─2943 nginx: master process /usr/sbin/nginx
└─2944 nginx: worker process
Jul 26 17:13:15 CentOSA systemd[1]: Stopped The nginx HTTP and reverse proxy server.
Jul 26 17:13:15 CentOSA systemd[1]: Starting The nginx HTTP and reverse proxy server...
Jul 26 17:13:15 CentOSA nginx[2938]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Jul 26 17:13:15 CentOSA nginx[2938]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Jul 26 17:13:15 CentOSA systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Jul 26 17:13:15 CentOSA systemd[1]: Started The nginx HTTP and reverse proxy server.
[root@CentOSA nginx]# netstat -atnup |grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2943/nginx: master
tcp 0 0 127.0.0.1:5000 0.0.0.0:* LISTEN 2808/python3
tcp6 0 0 :::80 :::* LISTEN 2943/nginx: master
访问网页报404
- 查看nginx日志
- 原因:selinux问题,关闭重启可以解决
- 再次访问
通过另外nginx代理服务访问
[root@CentOSB ~]# yum install nginx -y
[root@CentOSB ~]# vi /etc/nginx/nginx.conf
location / {
proxy_pass http://192.168.0.160:80;
}
[root@CentOSB ~]# systemctl restart nginx
[root@CentOSB ~]# systemctl enable nginx