[fly-iot] (11): The first step of converting the Postgres database into a MySQL database is to convert the SQL first, and then modify the database link. You can switch the code and initialize the data.

15587397:

foreword


The link to the original text of this article is:
https://blog.csdn.net/freewebsys/article/details/130673631
fly-iot column:
https://blog.csdn.net/freewebsys/category_12219758.html

Not to be reproduced without permission of the blogger.
The blogger’s CSDN address is: https://blog.csdn.net/freewebsys
The blogger’s nugget address is: https://juejin.cn/user/585379920479288
The blogger’s Zhihu address is: https://www.zhihu. com/people/freewebsystem

0, About the actorcloud project


Regarding the actorcloud project, reorganize the code of the open source project
Original project: https://github.com/actorcloud/ActorCloud

The projects are all open source using the Apache protocol.
The previous projects are also open source under the Apache protocol.

ActorCloud is an IoT platform for low-power IoT networks that provides one-stop platform services for enterprises. On the basis of safety and reliability, ActorCloud provides devices with communication capabilities for multiple protocol access, device data and message flow management functions.

The platform provides basic device management functions to connect and manage a large number of devices, realize device message communication and data collection persistence; integrate rule engine and data visualization management, flexibly open management and control APIs of various authority levels, and quickly develop the upper layer through APIs application to realize multi-terminal access and remote control of equipment.

IoT Hub: establish a reliable two-way connection channel for terminals on the cloud, perform authentication, protocol analysis, and message routing;
device management: terminal registration and life cycle management, providing continuous monitoring of status, faults, and traffic;
data engine: on High-speed persistence, real-time analysis, rule transaction processing and visual display of acquired terminal messages;
application enablement: provide terminal SDK, APP SDK, open and rich REST API interface, and integrate message push interface.

About IOT project address:
Project address:

Front-end project address:
https://gitee.com/fly-iot/fly-iot-frontend
Back-end project address:
https://gitee.com/fly-iot/fly-iot-backend-python
docker-compose project address :
https://gitee.com/fly-iot/docker-compose

1. About the Postgres database


The performance of the database is really good, but it doesn't feel too popular.
At the same time, the database in the cloud has TIDB that can support the Mysql driver. It is also very convenient to expand.
The main reason is that there are too many people using Mysql.

PostgreSQL is an object-relational database management system (ORDBMS) based on POSTGRES, version 4.2 developed by the Department of Computer Science, University of California, Berkeley. Many of the concepts that POSTGRES pioneered appeared much later in some commercial database systems.

PostgreSQL is the open source successor to the original Berkeley code. It supports most of the SQL standard and offers many modern features:

Complex Queries
Foreign Keys
Triggers
Updatable Views Transactional
Integrity
Multiversion Concurrency Control
Likewise, PostgreSQL can be extended in many ways, for example, by adding new:

Data types
Functions
Operators
Aggregate functions
Index methods
Procedural languages
​​And, thanks to the permissive license, PostgreSQL is free for anyone to use, modify, and distribute for any purpose, whether for private, commercial, or academic research purposes.
http://www.postgres.cn/docs/14/intro-whatis.html

2. Migration method


In fact, SQL is standard, but each database vendor implements it in a different way.
Postgres has a method similar to oracle to generate the primary key Id through secquence.
and some special fields. Just perform string replacement directly.

The first is the Postgres database file export:

-- 只是将数据表结构和索引导出:
pg_dump -U actorcloud -l actorcloud > all.sql

Remove without time zone,
quotation mark problem
Remove character varying() and replace it with varchar()

timestamp , changed to: timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,

Remove ::smallint,
modify jsonb to json,

Then similarly modify the SQL to:

Postgres SQL

CREATE TABLE public.actions (
    id integer NOT NULL,
    "createAt" timestamp without time zone,
    "updateAt" timestamp without time zone,
    "actionName" character varying(50),
    "actionType" smallint,
    description character varying(300),
    config jsonb,
    "userIntID" integer,
    "tenantID" character varying
);

Modify to MySQL:

CREATE TABLE actions (
    `id` bigint(20) NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `createAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updateAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `actionName` varchar(50),
    `actionType` smallint,
    description varchar(300),
    config json,
    `userIntID` integer,
    `tenantID` varchar(200) 
);

You can see that there is still a json method in MySQL version 5.7.

Additional SQL references:

docker-compose project address:
https://gitee.com/fly-iot/docker-compose/blob/master/mysql-table-schema.sql

insert image description here
There are 54 tables in total.

insert image description here

There are 54 tables in total:

`actions`, `actor_tasks`, `alembic_version`, `app_api_logs`, `app_api_logs_day`, `app_api_logs_hour`, `app_api_logs_month`, 

`applications`, `applications_groups`, `certs`, `certs_devices`, `codec`, `connect_logs`, `current_alerts`,

 `data_points`, `data_streams`, `device_count_day`, `device_count_hour`, `device_count_month`, `device_events`, 

`device_events_day`, `device_events_hour`, `device_events_latest`, `device_events_month`, `devices`, `dict_code`, `emqx_bills`, 

`emqx_bills_day`, `emqx_bills_hour`, `emqx_bills_month`, `end_devices`, `groups`, `groups_devices`, `history_alerts`, 

`invitations`, `login_logs`, `lwm2m_items`, `lwm2m_objects`, `messages`, `permissions`, `products`, `publish_logs`, `resources`, 

`roles`, `rules`, `rules_actions`, `services`, `streams_points`, `system_info`, `tenants`, `timer_publish`, `upload_info`,

 `users`, `users_groups`

3. Database link configuration


import asyncpg

from actor_libs.database.async_db import db
from actor_libs.tasks.timer import App
from .api_count import api_count_task
from .config import project_config
from .device_count import device_count_task
from .device_events import device_events_aggr_task
from .emqx_bills import emqx_bills_aggr_task
from .timer_publish import timer_publish_task


__all__ = ['app']


app = App(node_id='timer_task')


@app.on_event('startup')
async def open_database_connection_poll():
    _pool = await asyncpg.create_pool(
        host=project_config.get('POSTGRES_HOST', 'localhost'),
        port=project_config.get('POSTGRES_PORT', 5432),
        user=project_config.get('POSTGRES_USER', 'actorcloud'),
        password=project_config.get('POSTGRES_PASSWORD', 'public'),
        database=project_config.get('POSTGRES_DATABASE', 'actorcloud'),
        min_size=5, max_size=10
    )
    await db.open(_pool)
    

You can see that the connection pool of the postgres database is used. The driver is configured in the yaml configuration.

4. Summary


There is still a lot to do. Database initialization.
At the same time configure the docker-compse file to initialize the database.

insert image description here

################## mysql 数据库 5.7 版本 ##################
  mysql-iot:
    restart: always
    image: mysql:5.7
    container_name: mysql-iot
    ports:
        - "3306:3306"
    volumes:
        - "./data/mysql/data:/var/lib/mysql"
        - "./mysql/mysql.cnf:/etc/mysql/conf.d/mysql.cnf"
        - "./mysql/init.sql:/docker-entrypoint-initdb.d/init.sql"
        - "./mysql/connDB.sh:/connDB.sh"
    environment:
        MYSQL_ROOT_PASSWORD: mysqliot
        MYSQL_DATABASE: fly_iot
        TZ: Asia/Shanghai
    command: [
        '--character-set-server=utf8mb4',
        '--collation-server=utf8mb4_general_ci',
        '--max_connections=3000'
    ]

Just configure the script in this directory to automatically initialize the database:
/docker-entrypoint-initdb.d/init.sql
and then you can connect, and the code is changed to a mysql link in the configuration.
At the same time, add the mysql driver:

apt install -y gcc  default-libmysqlclient-dev
pip3 install mysqlclient

Then you can register/login successfully, but you don't have permission.

insert image description here

Continue to study the code.

The original link of this article is:
https://blog.csdn.net/freewebsys/article/details/130673631

insert image description here

Guess you like

Origin blog.csdn.net/freewebsys/article/details/130673631