From 9341c0e9b786f995c990a2abcf104c4358612ecd Mon Sep 17 00:00:00 2001 From: AgentLabCn Date: Fri, 15 Sep 2023 01:15:42 +0800 Subject: [PATCH] add README.md and db connection test, but not finished yet --- README.md | 25 +++++++++++++++++++++++++ SQL_db_user_create.sql | 6 ++++++ conf_db.yaml | 10 ++-------- controller_db.py | 11 +++++++++++ orm.py | 3 +-- 5 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 README.md create mode 100644 SQL_db_user_create.sql diff --git a/README.md b/README.md new file mode 100644 index 0000000..7577a5f --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +## 安装内容 +1. 数据库,推荐使用mysql 8.0以上版本 +2. Python 3.8 +3. 通过pip等方法安装*requirements_manual_selected_20230304.txt*文件中的包 + +## 前期准备工作 +1. 复制整个代码到本地 +2. 用root及密码登录mysql,在本地数据库中创建一个数据库,命名为*iiabmdb* +3. 在mysql中运行*SQL_db_user_create.sql*里的sql命令,创建数据库用户。如果创建用户报错,需打开该文件,并运行第三行被注释掉的代码。该文件后面的sql命令也需要运行,将数据库用户的权限赋予*iiabmdb*数据库 +4. 之后直接运行controller.py文件,如果没有报错,则说明前期准备工作完成 + +## 运行程序 +1. 将*conf_db_prefix.yaml*文件中的*db_prefix*改为*db_name_prefix: without_exp* +2. 打开命令行,进入代码所在目录,运行 +```shell +python main.py --exp without_exp --reset_db True +``` +3. 等待运行完成(23750个样本)。结束后,将*db_name_prefix: without_exp*改为*db_name_prefix: with_exp*,并运行 +```shell +python main.py --exp with_exp --reset_db True +``` +4. 漫长的等待(17.1万个样本),直到运行完成 + +## 获得结果,绘制图表 + diff --git a/SQL_db_user_create.sql b/SQL_db_user_create.sql new file mode 100644 index 0000000..eaef3bd --- /dev/null +++ b/SQL_db_user_create.sql @@ -0,0 +1,6 @@ +CREATE USER 'iiabm_user'@'localhost' IDENTIFIED WITH authentication_plugin BY 'iiabm_pwd'; + +-- CREATE USER 'iiabm_user'@'localhost' IDENTIFIED BY 'iiabm_pwd'; + +GRANT ALL PRIVILEGES ON iiabmdb.* TO 'iiabm_user'@'localhost'; +FLUSH PRIVILEGES; \ No newline at end of file diff --git a/conf_db.yaml b/conf_db.yaml index f1618a7..74e37e2 100644 --- a/conf_db.yaml +++ b/conf_db.yaml @@ -2,15 +2,9 @@ is_local_db: True local: - user_name: iiabm_yz - password: iiabm_yz + user_name: iiabm_user + password: iiabm_pwd db_name: iiabmdb address: 'localhost' port: 3306 -remote: - user_name: iiabm_yz - password: iiabm_yz - db_name: iiabmdb - address: 'localhost' - port: 3307 diff --git a/controller_db.py b/controller_db.py index 9965334..2901ea5 100644 --- a/controller_db.py +++ b/controller_db.py @@ -257,3 +257,14 @@ class ControllerDB: def lock_the_sample(sample: Sample): sample.is_done_flag, sample.computer_name = 0, platform.node() db_session.commit() + + +if __name__ == '__main__': + print("Testing the database connection...") + try: + controller_db = ControllerDB('test') + Base.metadata.create_all(bind=engine) + except Exception as e: + print("Failed to connect to the database!") + print(e) + exit(1) diff --git a/orm.py b/orm.py index f497b0e..80919b6 100644 --- a/orm.py +++ b/orm.py @@ -27,8 +27,7 @@ str_login = 'mysql://{}:{}@{}:{}/{}'.format(dct_conf_db['user_name'], dct_conf_db['address'], dct_conf_db['port'], dct_conf_db['db_name']) -print('DB is {}:{}/{}'.format(dct_conf_db['address'], - dct_conf_db['port'], dct_conf_db['db_name'])) +# print('DB is {}:{}/{}'.format(dct_conf_db['address'], dct_conf_db['port'], dct_conf_db['db_name'])) # must be null pool to avoid connection lost error engine = create_engine(str_login, poolclass=NullPool)