[原创]开源SQL审核平台——Archery 安装、部署心得

v1.2.0 → v1.3.8

0.软件版本及项目地址

1.安装python venv环境

1.1.安装 python36

yum install -y python36 python36-devel python-devel openldap-devel

cd /tmp
wget https://bootstrap.pypa.io/get-pip.py
python3.6 get-pip.py

1.2.创建 Python venv 环境(Python>=3.6.5,建议使用虚拟环境 )

# 创建 vevn
pip3 install virtualenv
cd /opt/
virtualenv venv4archery --python=python36
source /opt/venv4archery/bin/activate

which mysql_config     #确认命令在 $PATH 中,否则安装包时报错
cd /opt

# 安装 gcc
yum install -y gcc

# 下载 archery 源码
wget https://github.com/hhyo/archery/archive/v1.3.8.tar.gz
tar -zxvf v1.3.8.tar.gz
cd archery138/
pip3 install -r requirements.txt -i https://mirrors.ustc.edu.cn/pypi/web/simple/

1.3.报错处理参考

1.3.1.mysql_config 找不到报错

1.3.2.openldap-devel 包未安装报错

2.组件安装配置

2.1.MySQL 安装、配置

  • MySQL 安装略

  • 连接信息:

  • archery:

    • ip:127.0.0.1
    • port:3306
  • inception:

    • ip:127.0.0.1
    • port:3307

2.2.数据库账号授权

# 创建 archery 1.38 版本数据库
create database archery138 charset utf8mb4;

# 创建 archery 后台数据库账号
create user archery@'127.0.0.1' identified by 'xxxxxx';
grant all on archery138.* to archery@'127.0.0.1';

# 创建线上 v1.2.0 版本数据库恢复库
create database archery120to138 charset utf8mb4;

# 创建 inception 备份库连接账号
create user incep@'127.0.0.1' identified by 'xxxxxx';
grant all on *.* to incep@'127.0.0.1';

flush privileges;

2.3.inception 安装、配置、启动

  • inception 编译安装略

  • inception 配置文件

[inception]
general_log=1
general_log_file=inception.log
port=6669
socket=/tmp/inc.socket
character-set-client-handshake=0
character-set-server=utf8
inception_remote_system_password=123456
inception_remote_system_user=incep
inception_remote_backup_port=3306
inception_remote_backup_host=127.0.0.1
inception_support_charset=utf8,utf8mb4
inception_osc_on=ON
inception_osc_bin_dir=/usr/local/bin
inception_enable_nullable=0
inception_check_primary_key=1
inception_check_column_comment=1
inception_check_table_comment=1
inception_osc_min_table_size=1
inception_osc_chunk_time=0.1
inception_ddl_support=1
inception_enable_blob_type=1
inception_check_column_default_value=1

# 启动 inception
inception --defaults-file=incep.cnf &

2.4.Percona-toolkit 安装

cd /tmp
wget -r -np -nd --accept=gz --no-check-certificate https://www.percona.com/downloads/percona-toolkit/3.0.13/binary/tarball/percona-toolkit-3.0.13_x86_64.tar.gz
tar zxvf percona-toolkit-3.0.13_x86_64.tar.gz
cp percona-toolkit-3.0.13/bin/* /usr/local/bin/

2.5.Nginx 安装、配置

yum install -y nginx
vim /etc/nginx/conf.d/nginx.conf
server{
        listen 9123; #监听的端口
        server_name archery138;
        proxy_read_timeout 600s;     #超时时间与gunicorn超时时间设置一致,主要用于在线查询
        location / {
          proxy_pass http://127.0.0.1:8888;
          proxy_set_header Host $host:9123;      #解决重定向404的问题
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto $scheme;
        }

        location /static {
          alias /opt/archery138/archery/common/static;        #此处指向settings.py配置项STATIC_ROOT目录的绝对路径,用于nginx收集静态资源
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

2.6.上传 archery 第三方工具

cd /opt/
mkdir archery_tools
cd /opt/archery_tools
rz soar

2.7. SQLAdvisor 安装

git clone https://github.com/Meituan-Dianping/SQLAdvisor.git

yum install -y cmake libaio-devel libffi-devel glib2 glib2-devel gcc-c++ bison
yum install -y Percona-Server-shared-56  [ --enablerepo=Percona56 ]

# 如果不能安装 Percona-Server-shared-56 报错找不到包,那么需要执行下面语句
yum install http://www.percona.com/downloads/percona-release/redhat/0.1-3/percona-release-0.1-3.noarch.rpm 

cd SQLAdvisor
cmake -DBUILD_CONFIG=mysql_release -DCMAKE_BUILD_TYPE=debug -DCMAKE_INSTALL_PREFIX=/usr/local/sqlparser ./

make && make install
cd sqladvisor
cmake -DCMAKE_BUILD_TYPE=debug ./
make

cp /opt/archery_tools/SQLAdvisor/sqladvisor/sqladvisor /opt/archery_tools/
yes | rm /opt/archery_tools/SQLAdvisor -r

# 在本路径下生成一个sqladvisor可执行文件,这即是我们想要的。
/opt/archery_tools/sqladvisor --help

2.7.1.报错处理

  • 安装 Percona-Server-shared-56 报错,与 MariaDB-common-10.1.37-1.el7.centos.x86_64 包有冲突

  • 因为机器部署时使用的 MariaDB-common-10.1.37-1.el7.centos.x86_64 包安装的 zabbix-agent

  • 解决办法:从测试环境拷贝 libmysqlclient.so.18.1.0 文件,再创建软链接文件

2.8. SchemaSync 安装

cd /opt/archery_tools/
virtualenv venv4schemasync --python=python2
source venv4schemasync/bin/activate

git clone https://github.com/hhyo/SchemaSync.git
git clone https://github.com/hhyo/SchemaObject.git

cd SchemaObject && python setup.py install
cd ../SchemaSync && python setup.py install

yum install -y python-devel
pip install mysql-python

schemasync --version
which schemasync
cd ..
ln -s /opt/archery_tools/venv4schemasync/bin/schemasync schemasync

3.安装 archery 环境

3.1. 1.2.步已下载源码,略过

3.2.修改 archery/settings.py 文件 DATABASES 配置项

vim /opt/archery138/archery/settings.py
# 该项目本身的mysql数据库地址
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'archery138',     #archery信息库,执行py脚本生成
        'USER': 'archery',     #archery 特权登录用户,执行py脚本生成
        'PASSWORD': 'xxx',     #archery 特权登录用户密码
        'HOST': '10.xx.xx.xx',      #archery 数据库ip
        'PORT': '3306',     #archery 数据库端口
        'OPTIONS': {
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
            'charset': 'utf8mb4'    #archery 数据库字符集,默认为utf8mb4
        },
        'TEST': {
            'NAME': 'test_archery',
            'CHARSET': 'utf8',   #archery 数据库字符集,默认为utf8
        },
    }
}

# LDAP
ENABLE_LDAP = False # 测试时可以设置为 false,生产启用 LDAP 时开启
if ENABLE_LDAP:
    import ldap
    from django_auth_ldap.config import LDAPSearch
    AUTHENTICATION_BACKENDS = (
        'django_auth_ldap.backend.LDAPBackend',  # 配置为先使用LDAP认证,如通过认证则不再使用后面的认证方式
        'django.contrib.auth.backends.ModelBackend',  # django系统中手动创建的用户也可使用,优先级靠后。注意这2行的顺序
    )
    AUTH_LDAP_SERVER_URI = "ldap://xx.xx.xx.com"
    AUTH_LDAP_BIND_DN = "CN=auth,CN=Users,DC=xx,DC=xx,DC=com"
    AUTH_LDAP_BIND_PASSWORD = "xxxxxxxxxxxxxxx"
    AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=xxx,dc=xx,dc=xxx,dc=com",ldap.SCOPE_SUBTREE, "(sAMAccountName=%(user)s)")
    AUTH_LDAP_ALWAYS_UPDATE_USER = True  # 每次登录从ldap同步用户信息
    AUTH_LDAP_USER_ATTR_MAP = {  # key为archery.sql_users字段名,value为ldap中字段名,用户同步信息
        "username": "xxxxxxxxxxxx",
        "display": "cn",
        "email": "mail"
    }

3.3.数据库初始化

cd /opt/archery138/
python36 manage.py makemigrations sql   #这一步如果遇到报错可忽略
python36 manage.py migrate

3.4.创建管理用户

cd /opt/archery138/
python3 manage.py createsuperuser
Username: archery_admin    # 手动输入
Email address: dba-notice@ybm100.com
Password: xxxx       #输入密码
Password (again): xxxxx      #确认密码
Superuser created successfully.

3.5.调试启动(runserver 方式)

source /opt/venv4archery/bin/activate
python3 manage.py runserver 0.0.0.0:9123 --insecure &

3.6.安装gunicorn

  • venv中已经安装,无需额外安装

3.7.gunicorn + nginx 启动(正式环境启动方式)

cd /opt/archery138/
source /opt/venv4archery/bin/activate
sh startup.sh &

3.8.一键启动脚本,启动 archery

#!/bin/bash
inception --defaults-file=/opt/inception/inc.cnf &
source /opt/venv4archery/bin/activate
cd /opt/archery138
python3 /opt/archery138/manage.py runserver 0.0.0.0:9123 --insecure &

sh /opt/archery138/startup.sh &

4.数据库升级

4.1.导出线上库

# 10.xx.xx.xx
sudo su -
mysqldump -S xx/mysql.sock --single-transaction --master-data=2 --set-gtid-purged=OFF db_ops > db_ops.sql

4.2.在目标实例上恢复数据

# 10.xx.xx.xx
sudo su -
mysql -S xx/mysql.sock db_ops <db_ops.sql

4.3.执行数据库升级脚本

# 10.xx.xx.xx
sudo su -
cd /opt/archery138/src/init_sql
mysql -h 10.xx.xx.xx -P3306 -uxx -p db_ops < v1.2.0_1.3.0.sql
mysql -h 10.xx.xx.xx -P3306 -uxx -p db_ops < v1.3.0_1.3.2.sql
mysql -h 10.xx.xx.xx -P3306 -uxx -p db_ops < v1.3.6_v1.3.7.sql

4.4.导出恢复库升级数据,并导入 archery 数据库

# 10.xx.xx.xx
sudo su -
mysqldump -S xx/mysql.sock --single-transaction --master-data=2 --set-gtid-purged=OFF --no-create-info db_ops > updated_db_data.sql.bak
mysql -h 10.xx.xx.xx -P3306 -uxx -p archery < updated_db_data.sql,bak

5.配置archery

访问 http://10.xx.xx.xx:9123

“系统管理”-->“配置项管理”

Inception配置

——填写完成后点击测试,通过后保存

INCEPTION_HOST 10.xx.xx.xx
INCEPTION_PORT 6669
REMOTE_BACKUP_HOST 10.xx.xx.xx
REMOTE_BACKUP_PORT 3306
REMOTE_BACKUP_USER incep
REMOTE_BACKUP_PASSWORD xxxx

功能模块配置

SQL****查询
QUERY ON
SQL优化
SQLADVISOR_PATH /opt/archery_tools/sqladvisor
SOAR_PATH /opt/archery_tools/soar
SOAR_TEST_DSN xxx:xxx@10.xx.xx.xx:3306/archery

通知配置

——填写完成后点击测试,通过后保存

MAIL ON
MAIL_SSL ON
MAIL_SMTP_SERVER xxx
MAIL_SMTP_PORT xxx
MAIL_SMTP_USER xxx
MAIL_SMTP_PASSWORD xxx
DDL_NOTIFY_AUTH_GROUP dinglu@ybm100.com
DING ON

其他配置

SCHEMASYNC /opt/archery_tools/venv4schemasync/bin/schemasync

6.Bug修复

6.1. vim sql/instance.py

  • 如果在archery平台上使用schemasync进行表对比的时候出现问题,可能是登录mysql的密码中出现了特殊字符,使得schemasync

  • 无法识别这个密码,可以将含有特殊字符的密码改成不含有特殊字符的密码,问题就可以解决了,或者是修改instance.py 文件中的代码

修改前:

command = path + ' %s --output-directory=%s --tag=%s \
    mysql://%s:%s@%s:%d/%s  mysql://%s:%s@%s:%d/%s'

修改后:

command = path + " %s --output-directory=%s --tag=%s \
    mysql://%s:'%s'@%s:%d/%s  mysql://%s:'%s'@%s:%d/%s" 
    # 将其中的密码用单引号引起来就可以了
posted @ 2019-07-10 18:28  指弹小能手  阅读(11947)  评论(0编辑  收藏  举报

关于作者:专注数据库、Python 开发、ROS、深度学习,请多多赐教!

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接 如有问题, 可邮件(iamkuboy@163.com)咨询.