冯梦华的日常

[NFS] mount: Connection timed out

问题描述

Linux 服务端和 Arm 开发板客户端进行 NFS 服务的连接。

Linux 和 Arm 开发板之间能ping通,并且处于同一网端,且掩码、网口相同,但是在执行下面的程序时发生超时错误。

在服务端的nfs配置完全正确的情况下,Arm开发板还是无法正确连接nfs服务器。

[root@FORLINX6410]# mount -t nfs -o nolock,hard 192.168.1.5:/home/yarnom/nfs /mnt
mount: mounting 192.168.1.5:/home/yarnom/nfs on /mnt failed: Connection timed out

解决

这个问题困扰了我两天终于在这个帖子里找到了解决方案。

Mount the NFS filesystem using the TCP protocol instead of the default UDP protocol. Many NFS servers only support UDP.

这是在NFSv3中添加了对TCP协议的支持:

总之,我尝试了下面的命令,使用了tcp协议:

$ mount -t nfs -o nolock,proto=tcp,port=2049 192.168.1.5:/home/yarnom/nfs /mnt

这个协议最终使我正确连接上了nfs服务器。

工程 [2023_3_25] 附录文件「二」 - Flask SQLAlchemy

前言

本文主要介绍 Flask SQLAlchemy 的具体使用。

如需要了解 Flask 入门文档可跳转:

Setup

Install MySQL/MariaDB

1. Installation

MariaDB is the default implementation of MySQL in Arch Linux, provided with the mariadb package.

Install mariadb, and run the following command before starting the mariadb.service

# mariadb-install-db --user=mysql --basedir=/usr --datadir=/var/lib/mysql

2. Configuration

Once you have started the MariaDB server and added a root account, you may want to change the default configuration.

To log in as root on the MariaDB server, use the following command:

# mariadb -u root -p
2.1 Add user

Creating a new user takes two steps: create the user; grant privileges. In the below example, the user monty with some_pass as password is being created, then granted full permissions to the database mydb:

# mariadb -u root -p
MariaDB> CREATE USER 'monty'@'localhost' IDENTIFIED BY 'some_pass';
MariaDB> GRANT ALL PRIVILEGES ON mydb.* TO 'monty'@'localhost';
MariaDB> quit

安装 PyMySQL 和 Flask-SQLAlchemy

使用python 提供 pip 包管理器安装 pymysqlFlask-SQLAlchemy

$ pip install PyMySQL
$ pip install flask-sqlalchemy

Connection URI Format

For a complete list of connection URIs head over to the SQLAlchemy documentation under (Supported Databases). This here shows some common connection strings.

SQLAlchemy indicates the source of an Engine as a URI combined with optional keyword arguments to specify options for the Engine. The form of the URI is:

dialect+driver://username:password@host:port/database

MySQL:

mysql://scott:tiger@localhost/mydatabase

Configuration Keys

SQLALCHEMY_DATABASE_URI

The database URI that should be used for the connection. Examples:

  • sqlite:////tmp/test.db
  • mysql://username:password@server/db

创建数据库

安装完上边的套件后,就可以正式创建Mysql数据库了。

使用root用户创建数据库

sudo mariadb -u root -p

进入mariadb后,使用如下命令创建数据库:

MariaDB [(none)]> create database proj_20230325;
Query OK, 1 row affected (0.000 sec)

给予权限

之后给予用户 Yarnom 该数据库的所有权限:

MariaDB [(none)]> GRANT ALL PRIVILEGES ON proj_20230325.* TO 'yarnom'@'localhost';
Query OK, 0 rows affected (0.009 sec)

安装 FLask-Migrate

$ pip install Flask-Migrate

配置

from flask_migrate import Migrate
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+pymysql://yarnom:root@localhost:3306/proj_20230325"
db = SQLAlchemy(app)
Migrate(app,db)

初始化

$ flask db init

Flask 使用Mysql数据库

创建出相应的模板:

class Student(db.Model):
    id = db.Column('id', db.String(100),primary_key=True)
    name = db.Column('name',db.String(100))
    def __init__(self, id, name):
        self.id =id
        self.name = name

使用如下命令更新数据库

$ flask db migrate -m "說明文字"
$ flask db upgrade

Flask-Sqlalchemy 使用

以下内容转载自flask-sqlalchemy 官方文档

Flask 添加数据

me = User('admin', 'admin@example.com')
db.session.add(me)
db.session.commit()

Flask 查询数据

So how do we get data back out of our database? For this purpose Flask-SQLAlchemy provides a query attribute on your Model class. When you access it you will get back a new query object over all records. You can then use methods like filter() to filter the records before you fire the select with all() or first(). If you want to go by primary key you can also use get().

The following queries assume following entries in the database:

idusernameemail
1adminadmin@example.com
2peterpeter@example.org
3guestguest@example.com

Retrieve a user by username:

>>> peter = User.query.filter_by(username='peter').first()
>>> peter.id
2
>>> peter.email
u'peter@example.org'

Same as above but for a non existing username gives None:

>>> missing = User.query.filter_by(username='missing').first()
>>> missing is None
True

Selecting a bunch of users by a more complex expression:

>>> User.query.filter(User.email.endswith('@example.com')).all()
[<User u'admin'>, <User u'guest'>]

Ordering users by something:

>>> User.query.order_by(User.username).all()
[<User u'admin'>, <User u'guest'>, <User u'peter'>]

Limiting users:

>>> User.query.limit(1).all()
[<User u'admin'>]

Getting user by primary key:

>>> User.query.get(1)
<User u'admin'>

Debtap

Setup

==> You must run at least once "debtap -u"
with root privileges (preferably recently),
before running this script

==> Syntax: debtap [option] package_filename

==> Run "debtap -h" for help

Update debtap source

$ debtap -u

Convert deb package

debtap xxx.deb

Install

sudo pacman -U xxx.pkg

工程 [2023_3_25] 附录文件「一」 - Flask 起步

前言

因为前段时间实验室老师要求我完成一个项目,是有关学校教务系统的,要求完成一个微信小程序。

目前处于实验阶段,老师也没有给我需求文档,所以目前的情况就是做些东西练练手。这里记录一下,之后正式工作了能快速完成基本的构建。

以下内容转载自 Flask官方文档

Flask Installation

Create an environment

Create a project folder and a venv folder within:

$ mkdir myproject
$ cd myproject
$ python3 -m venv venv

Activate the environment

Before you work on your project, activate the corresponding environment:

$ . venv/bin/activate

Install Flask

Within the activated environment, use the following command to install Flask:

$ pip install Flask

Quickstart

A minimal Flask application looks something like this:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"

So what did that code do?

  1. First we imported the Flask class. An instance of this class will be our WSGI application.

  2. Next we create an instance of this class. The first argument is the name of the application’s module or package. __name__ is a convenient shortcut for this that is appropriate for most cases. This is needed so that Flask knows where to look for resources such as templates and static files.

  3. We then use the route() decorator to tell Flask what URL should trigger our function.

  4. The function returns the message we want to display in the user’s browser. The default content type is HTML, so HTML in the string will be rendered by the browser.

Save it as hello.py or something similar. Make sure to not call your application flask.py because this would conflict with Flask itself.

To run the application, use the flask command or python -m flask. You need to tell the Flask where your application is with the --app option.

$ flask --app hello run
 * Serving Flask app 'hello'
 * Running on http://127.0.0.1:5000 (Press CTRL+C to quit)

Externally Visible Server

If you run the server you will notice that the server is only accessible from your own computer, not from any other in the network. This is the default because in debugging mode a user of the application can execute arbitrary Python code on your computer.

If you have the debugger disabled or trust the users on your network, you can make the server publicly available simply by adding --host=0.0.0.0 to the command line:

$ flask run --host=0.0.0.0

This tells your operating system to listen on all public IPs.

Debug

To enable debug mode, use the —debug option.

$ flask --app hello run --debug
 * Serving Flask app 'hello'
 * Debug mode: on
 * Running on http://127.0.0.1:5000 (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: nnn-nnn-nnn

Routing

Modern web applications use meaningful URLs to help users. Users are more likely to like a page and come back if the page uses a meaningful URL they can remember and use to directly visit a page.

Use the route() decorator to bind a function to a URL.

@app.route('/')
def index():
    return 'Index Page'

@app.route('/hello')
def hello():
    return 'Hello, World'

HTTP Methods

Web applications use different HTTP methods when accessing URLs. You should familiarize yourself with the HTTP methods as you work with Flask. By default, a route only answers to GET requests. You can use the methods argument of the route() decorator to handle different HTTP methods.

from flask import request

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        return do_the_login()
    else:
        return show_the_login_form()

The Request Object

The request object is documented in the API section and we will not cover it here in detail (see Request). Here is a broad overview of some of the most common operations. First of all you have to import it from the flask module:

from flask import request

The current request method is available by using the method attribute. To access form data (data transmitted in a POST or PUT request) you can use the form attribute. Here is a full example of the two attributes mentioned above:

@app.route('/login', methods=['POST', 'GET'])
def login():
    error = None
    if request.method == 'POST':
        if valid_login(request.form['username'],
                       request.form['password']):
            return log_the_user_in(request.form['username'])
        else:
            error = 'Invalid username/password'
    # the code below is executed if the request method
    # was GET or the credentials were invalid
    return render_template('login.html', error=error)

存储扩展

在使用了两年的双系统后,逐渐感觉到了512G是远远不够的,但暂时没有更换硬盘的资金,所以,我计划分别购入256G和128G的SD卡作为系统的扩展。

速度测试

由于不是 SSD 硬盘,而且 SD 卡被设计出来主要就是静态的存储,没有类似主控的东西,所以无论是速度还是质量都是难堪大任的,但由于我平时并不怎么使用 windows , 所以尚能接受。

但为了保险起见,我将使用一张 64G 的 tf 卡测试其最小性能。

64G 的 tf 卡满足整体的使用,所以我购买了一张 128G 的 SD 卡作为虚拟机的硬盘,显然在速度方面它的能够满足我的使用的,那么自要做好备份工作就没问题了。

闪迪 256G 固态U盘

事实上在这个容量上,买一盘固态硬盘更好,同样的价格可以来到500G的容量,而且安全性肯定也是更高的,但是考虑到U盘的体积,所以,我更偏爱于这个U盘。

对于这个U盘,我可能有以下打算:

1.仍然保留双系统,这个固态U盘作为虚拟机硬盘使用。这个打算主要是考虑到SD卡的使用寿命并不安全,很可能在我工作时崩坏,这会导致严重的后果。而这个U盘显然更加的安全一些。

2.不再保留双系统,Linux 系统将独占整个512G的硬盘,同时分离128G出来给Windows虚拟机,之前购买的128G的SD卡作为资料存储卡,256G的固态U盘将安装为 Windows 系统。

我更喜欢第二种方案,因为它更安全可靠。

大三下学期

时间真的过得很快,大一时会在校园迷路的我如今也在面临考研或者直接工作的两个方向,如今的倾向是考研,因为随着 Google 、字节 这些大厂的裁员情况来看,互联网行业并不景气,加之 AI 的高速发展,低技术人员被快速淘汰,可能,提升自己的学历会是一个更好的选择。

我的选择是广西大学,这是一个计算机 B- 的学校,综合下来是很适合我的情况的。