airflow安装及配置使用mysql作为数据库

1、我使用的是miniconda3创建的python3.7.12环境

2、配置mysql5.7环境;

#安装mysql依赖
sudo yum -y install python-devel libevent-devel mysql-devel mysqlclient
pip install mysqlclient pymysql mysql

#修改mysql参数
vi /etc/my.cnf
explicit_defaults_for_timestamp=1

#创建个airflow的用户,airflow数据库
mysql -uroot -p
CREATE DATABASE IF NOT EXISTS airflow DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
create user 'airflow'@'%' identified by 'xxxxxxxx';
grant all privileges on airflow.* to airflow@localhost identified by 'xxxxxxxx';
grant all privileges on airflow.* to 'airflow'@'%' identified by 'xxxxxxxx';
flush privileges;
select user,authentication_string,host from user;

3、安装及基本配置

#安装airflow,2.2.3
pip install apache-airflow
#配置个环境变量
vi ~/.bashrc
export AIRFLOW_HOME=/home/san/airflow
source ~/.bashrc
airflow version
#修改配置文件
vi /home/san/airflow/airflow.cfg
executor = LocalExecutor
sql_alchemy_conn = mysql://airflow:xxxxxxxx@localhost:3306/airflow?charset=
### Apache Airflow 安装、部署与使用指南 Apache Airflow 是一个用于编程、调度和监控工作流的平台,支持通过 Python 编程语言来创建和管理工作流[^1]。以下是关于如何安装、部署和使用 Apache Airflow 的详细说明。 --- #### 一、安装 Apache Airflow安装 Apache Airflow 之前,需要确保系统满足以下依赖条件: 1. **升级 PIP** 使用以下命令升级 pip 工具以确保其为最新版本: ```bash pip3 install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple ``` 2. **检查 Python 和 PIP 版本** 确认当前系统的 Python 和 PIP 版本是否符合要求: ```bash python3 -V pip3 -V ``` 3. **设置环境变量** 在 Ubuntu 系统中,可以通过以下命令设置 AIRFLOW_HOME 环境变量,指定 Airflow配置文件路径: ```bash export AIRFLOW_HOME=~/airflow ``` 4. **安装 Apache Airflow** 使用 pip 安装 Airflow,并指定所需的后端数据库驱动(例如 MySQL): ```bash pip install apache-airflow==2.10.5 pip install mysql-connector-python pymysql ``` --- #### 二、部署 Apache Airflow 完成安装后,需要对 Airflow 进行初始化和配置: 1. **初始化数据库** 默认情况下,Airflow 使用 SQLite 数据库,但建议替换为更强大的后端数据库(如 MySQL)。以下是将数据库切换到 MySQL 的步骤: - 创建 MySQL 数据库及用户: ```sql CREATE DATABASE airflow_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; CREATE USER 'airflow_user' IDENTIFIED BY 'airflow_pass'; GRANT ALL PRIVILEGES ON airflow_db.* TO 'airflow_user'; ``` - 修改 `airflow.cfg` 文件中的数据库连接字符串: ```ini sql_alchemy_conn = mysql+mysqldb://airflow_user:airflow_pass@localhost/airflow_db executor = LocalExecutor ``` - 迁移数据库结构: ```bash airflow db init ``` 2. **启动 Web 服务和调度程序** 启动 Airflow 的 Web 服务和调度程序以运行 DAG: ```bash airflow webserver -p 8080 & airflow scheduler & ``` --- #### 三、使用 Apache Airflow 1. **定义 DAG** DAG(Directed Acyclic Graph)是 Airflow 中的核心概念,用于描述任务的工作流。以下是一个简单的 Python 示例代码: ```python from airflow import DAG from airflow.operators.bash import BashOperator from datetime import datetime, timedelta default_args = { 'owner': 'airflow', 'depends_on_past': False, 'start_date': datetime(2023, 10, 1), 'retries': 1, 'retry_delay': timedelta(minutes=5), } with DAG('example_dag', default_args=default_args, schedule_interval='@daily') as dag: t1 = BashOperator( task_id='print_date', bash_command='date', ) t2 = BashOperator( task_id='sleep', bash_command='sleep 5', ) t1 >> t2 ``` 2. **监控和管理** - 访问 Airflow 的 Web UI(默认地址为 `http://localhost:8080`),可以查看 DAG 的运行状态、日志和历史记录。 - 使用 REST API 或 Python 客户端库 `apache-airflow-client-python` 对资源进行交互式管理[^2]。 --- #### 四、注意事项 - 如果遇到 SQLite 数据库的限制,建议按照上述步骤切换到 MySQL 数据库[^5]。 - 在生产环境中,推荐使用 CeleryExecutor 或 KubernetesExecutor 替代 LocalExecutor,以实现更高的并发能力。 - 确保所有依赖项已正确安装,避免因缺少驱动或库导致的问题[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值