【项目背景】
Python作为后台服务器开发的web设备管理系统,相关的服务有redis+mysql
Linux机器为全新的虚拟机,环境是空白的,所以需要全部重新安装。
【安装依赖】

   yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel
   yum -y install gcc cc

【安装python】
输入python检测是否有python:

[root@localhost Python-3.8.5]# python
Python 2.7.5 (default, Apr 11 2018, 07:36:10)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

发现已经有系统自带python2.7,我们需要安装python3.8,从官网下载python3.8.5并上传到目录:/home/python/

 tar -zxvf 解压 

执行命令开始安装:

 cd Python-3.8.0
./configure --prefix=/usr/local/python3
make && make install

建立软链,提示已经有软链存在,是由于已经连接python2 ,删除软链:

sudo rm /usr/bin/python

建立软链

ln -s /usr/local/python3/bin/python3.8 /usr/bin/python

再次查看python 检查版本。
同时将pip 软链:

ln -s /usr/local/python3/bin/pip3.8 /usr/bin/pip

【安装项目依赖】

  1. 将项目上传到目录:/home/python/
  2. 进入项目目录:/management_platform_backend/
  3. 使用pip安装所需要的依赖
  4. pip install -r requirements.txt -i https://pypi.doubanio.com/simple/
    在这里插入图片描述
    看到绿色的successful代表安装成功,其他网络原因请自行解决,一般是网络或者镜像问题。
    pip查看已经安装的依赖:
[root@localhost management_platform_backend]# pip list
Package               Version
--------------------- -----------
alembic               1.8.1
aniso8601             9.0.1
APScheduler           3.6.0
backports.zoneinfo    0.2.1
bcrypt                3.2.2
cffi                  1.15.1
click                 8.1.3
Flask                 1.0
Flask-Bcrypt          0.7.1
Flask-HTTPAuth        3.2.4
Flask-Login           0.4.1
Flask-Migrate         2.4.0
Flask-RESTful         0.3.7
Flask-Script          2.0.6
Flask-SQLAlchemy      2.3.2
Flask-WTF             0.14.2
importlib-metadata    4.12.0
importlib-resources   5.9.0
itsdangerous          1.1.0
Jinja2                2.10.1
Mako                  1.2.1
MarkupSafe            1.1.1
ntplib                0.4.0
pip                   20.1.1
psutil                5.6.2
pycparser             2.21
PyMySQL               0.9.3
python-dateutil       2.8.0
pytz                  2019.1
pytz-deprecation-shim 0.1.0.post0
redis                 3.2.1
setuptools            47.1.0
six                   1.16.0
SQLAlchemy            1.3.2
tzdata                2022.1
tzlocal               4.2
Werkzeug              0.14.1
WTForms               2.2.1
zipp                  3.8.1

【安装pip】

[root@localhost ~]# uwsgi
bash: uwsgi: command not found...
[root@localhost ~]# pip install uwsgi
Collecting uwsgi
  Downloading uwsgi-2.0.20.tar.gz (804 kB)
     |████████████████████████████████| 804 kB 94 kB/s
Using legacy setup.py install for uwsgi, since package 'wheel' is not installed.
Installing collected packages: uwsgi
    Running setup.py install for uwsgi ... done
Successfully installed uwsgi-2.0.20

进入项目目录 scripts脚本目录下:

cd /home/python/management_platform_backend/scripts/

运行脚本 启动项目提示无权限:

./start_script.sh
[root@localhost scripts]# ./start_script.sh
-bash: ./start_script.sh: Permission denied

Linux给文件夹赋权:

chmod +x -R scripts/

然后再次运行脚本提示可以看到如下错误日志,这是正常的,因为此时我们还没有安装数据库和redis:

Traceback (most recent call last):
  File "/usr/local/python3/lib/python3.8/site-packages/pymysql/connections.py", line 581, in connect
    sock = socket.create_connection(
  File "/usr/local/python3/lib/python3.8/socket.py", line 808, in create_connection
    raise err
  File "/usr/local/python3/lib/python3.8/socket.py", line 796, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/python3/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 2262, in _wrap_pool_connect
    return fn()
  File "/usr/local/python3/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 354, in connect
    return _ConnectionFairy._checkout(self)
  File "/usr/local/python3/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 751, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/usr/local/python3/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 483, in checkout
    rec = pool._do_get()
  File "/usr/local/python3/lib/python3.8/site-packages/sqlalchemy/pool/impl.py", line 138, in _do_get
    self._dec_overflow()
  File "/usr/local/python3/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "/usr/local/python3/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 129, in reraise
    raise value
  File "/usr/local/python3/lib/python3.8/site-packages/sqlalchemy/pool/impl.py", line 135, in _do_get
    return self._create_connection()
  File "/usr/local/python3/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 299, in _create_connection
    return _ConnectionRecord(self)
  File "/usr/local/python3/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 428, in __init__
    self.__connect(first_connect_check=True)
  File "/usr/local/python3/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 630, in __connect
    connection = pool._invoke_creator(self)
  File "/usr/local/python3/lib/python3.8/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
    return dialect.connect(*cargs, **cparams)
  File "/usr/local/python3/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 453, in connect
    return self.dbapi.connect(*cargs, **cparams)
  File "/usr/local/python3/lib/python3.8/site-packages/pymysql/__init__.py", line 94, in Connect
    return Connection(*args, **kwargs)
  File "/usr/local/python3/lib/python3.8/site-packages/pymysql/connections.py", line 325, in __init__
    self.connect()
  File "/usr/local/python3/lib/python3.8/site-packages/pymysql/connections.py", line 630, in connect
    raise exc
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 86, in <module>
    manager.run()

【安装mysql】
root下创建目录/root/JiuLue/install/Downloads/,并想Mysql 安装包上传到该目录下,通过脚本命令一键安装,脚本上传到/root/JiuLue/install/shell_scripts/ 目录下,脚本代码如下:

  #!/bin/bash
echo 'install mysql5.7 begin'
mkdir /usr/local/mysql
tar -xvf /root/JiuLue/install/Downloads/mysql-5.7.27-1.el7.x86_64.rpm-bundle.tar -C /usr/local/mysql
cd /usr/local/mysql
yum clean expire-cache
yum localinstall -y install mysql-community-{server,client,common,libs}-*
#createrepo ./
#cp /root/JiuLue/install/config_files/mysql.repo /etc/yum.repos.d/mysql.repo
#cd /etc/yum.repos.d/
#yum install mysql-server
echo 'LimitNOFILE = 102400' >> /etc/systemd/system/multi-user.target.wants/mysqld.service
systemctl daemon-reload
systemctl start mysqld
systemctl enable mysqld
echo 'mysql done'

拷贝以上代码 创建 install_mysql.sh脚本 运行即可。

[root@localhost shell_scripts]# ./install_mysql.sh
install mysql5.7 begin
mysql-community-libs-5.7.27-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.27-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.27-1.el7.x86_64.rpm
mysql-community-devel-5.7.27-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.27-1.el7.x86_64.rpm
mysql-community-common-5.7.27-1.el7.x86_64.rpm
mysql-community-client-5.7.27-1.el7.x86_64.rpm
mysql-community-server-5.7.27-1.el7.x86_64.rpm
mysql-community-test-5.7.27-1.el7.x86_64.rpm
mysql-community-embedded-5.7.27-1.el7.x86_64.rpm
  File "/usr/bin/yum", line 30
    except KeyboardInterrupt, e:
                            ^
SyntaxError: invalid syntax
  File "/usr/bin/yum", line 30
    except KeyboardInterrupt, e:
                            ^
SyntaxError: invalid syntax
Failed to start mysqld.service: Unit is not loaded properly: Invalid argument.
See system logs and 'systemctl status mysqld.service' for details.
Failed to execute operation: No such file or directory
mysql done

安装过程报错,这是因为yum包管理是使用python2.x写的,将python2.x升级到python3.1.3以后,由于python版本语法兼容性导致问题出现
解决办法:
修改yum配置文件,将python版本指向以前的旧版本
vi /usr/bin/yum
!/usr/bin/python2.7
安装依赖: yum install libaio perl net-tools
成功后 重启执行命令安装 mysql: ./install_mysql.sh
启动mysql:

  [root@localhost shell_scripts]# systemctl start mysqld

mysql 进入 mysql 提示密码错误, 需要重新设置密码,
编辑my.cnf文件 跳过密码校验,重新设置密码:
vim /etc/my.cnf
mysqld 下增加一行 skip-grant-tables

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip-grant-tables

进入mysql 使用mysql提示错误
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘database’ at line 1

重新刷新权限:
flush privileges;

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> set password for 'root'@'localhost' = password('Admin123');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> update user set host ='%' where user = 'root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit;

此时通过远程连接mysql 发现连不上, 已经设置了host为 %,并且已经关闭防火墙
排查问题:

  1. 通过命令查看3306端口是否开放 :
  2. netstat -an | grep 3306
3. [root@localhost shell_scripts]# netstat -an | grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN

.windows下窗口 telnet 远程是否通,发现连接失败
4. systemctl status firewall 防火墙关闭状态
5. 进入mysql 重新设置如下:
6. mysql> use mysql;

Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql>
mysql> grant all privileges on *.* to 'root'@'%' identified by 'Admin123' with grant option;
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql>
mysql> set global validate_password.policy=0;
ERROR 1193 (HY000): Unknown system variable 'policy'
mysql>
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Admin123';
ERROR 1396 (HY000): Operation ALTER USER failed for 'root'@'localhost'
mysql> ALTER USER 'root'@'%' IDENTIFIED BY 'Admin123';
Query OK, 0 rows affected (0.00 sec)

mysql> grant all privileges on *.* to 'root' @'%' identified by 'Admin123';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql>

重启还依旧远程连不上
查看Linux系统版本:
1.uname -a (Linux查看版本当前操作系统内核信息)
2.cat /proc/version (Linux查看当前操作系统版本信息)
3.cat /etc/issue 或cat /etc/redhat-release(Linux查看版本当前操作系统发行版信息)
4.cat /proc/cpuinfo lscpu (Linux查看cpu相关信息,包括型号、主频、内核信息等)
通过查看是 redhat 系统, redhat系统 需要selinux关闭
sestatus或者/usr/sbin/sestatus [-v] 查看状态:

修改 /etc/selinux/config 文件中的 SELINUX="enforcing" ,设置为 disabled ,然后重启系统。

再次通过远程连接mysql,显示连接成功,至此,mysql安装完成。
【Redis安装】
将redis包上传到/root/JiuLue/install/Downloads/
cd进入目录 :/root/JiuLue/install/shell_scripts/
运行redis安装脚本,redis脚本如下,直接复制代码新建shell文件启动脚本即可:

#!/bin/bash
echo 'install redis5.0.5 begin'
tar -xvf /root/JiuLue/install/Downloads/redis-5.0.5.tar.gz -C /usr/local/
cd /usr/local/ && mv redis-5.0.5 redis
cd /usr/local/redis/
make MALLOC=libc
cd src && make install
\cp -f /root/JiuLue/install/config_files/redis.conf /usr/local/redis/redis.conf
echo 'redis done'

运行脚本安装:
安装完成后启动redis:

cd /usr/local/bin
./redis-server &
修改redis.conf文件 为后台服务方式启动:
daemonize 的值改为 yes

重新启动redis
【启动项目】
该项目有两种启动方式:

  1. 运行script目录下的 start.sh脚本启动
  2. 通过uwsgi --ini uwsgi_dev.ini 启动
  3. 至此,项目成功部署完成!
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐