Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/waqqas/django-mysql-replication
MySQL DB change events by reading replication logs
https://github.com/waqqas/django-mysql-replication
Last synced: about 2 months ago
JSON representation
MySQL DB change events by reading replication logs
- Host: GitHub
- URL: https://github.com/waqqas/django-mysql-replication
- Owner: waqqas
- License: mit
- Created: 2024-05-12T13:13:15.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-05-12T14:52:28.000Z (8 months ago)
- Last Synced: 2024-09-19T04:38:23.786Z (4 months ago)
- Language: Python
- Size: 12.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
### django-myql-migration
MySQL DB change events by reading replication logs. These are useful when the data is directly inserted into the MySQL database.### Installation
Install via `pip`
`pip install django-myql-migration`
Add `"django_myql_migration"` to your `INSTALLED_APPS` settings like this:
```
INSTALLED_APPS = (
"django_myql_migration",
...
)
```Edit `my.cnf` to setup replication, as follows:
```
[mysqld]
log-bin=mysql-bin
server-id=1
binlog-format=row
gtid_mode=ON
log-slave_updates=true
enforce_gtid_consistency
binlog-row-metadata=FULL
binlog-row-image=FULL
```Set these environment variables `MYSQL_HOST`, `MYSQL_PORT`, `MYSQL_DATABASE`, `MYSQL_SLAVE_USER`, `MYSQL_SLAVE_PASSWORD`
Add replication user using the following command
`mysql -u root -p -h ${MYSQL_HOST} -P ${MYSQL_PORT} ${MYSQL_DATABASE} -e "CREATE USER '${MYSQL_SLAVE_USER}'@'%' IDENTIFIED BY '${MYSQL_SLAVE_PASSWORD}'; GRANT REPLICATION SLAVE, REPLICATION CLIENT, SELECT ON *.* TO '${MYSQL_SLAVE_USER}'@'%';FLUSH PRIVILEGES;"`
### Usage
There are 3 signals generated by the package: `row_inserted`, `row_deleted` and `row_updated`. You can use the standard Django signal handling mechanism to handle signals. e.g. To know when the user is added to the system, you can create a `signals.py` file with the following content
```
from django_mysql_replication.signals import row_inserted
from django.contrib.auth import get_user_modelUser = get_user_model()
@receiver(row_inserted, sender=User)
def user_added(sender, instance, *args, **kwargs):
pass```
Run the following command to listen to DB changes
`python manage.py listen`