当前位置:   article > 正文

Yearning做SQL审核_yarning alter table

yarning alter table

 系统环境: Centos7
一、Inception安装

1、安装相关依赖包
yum install bison ncurses-libs libncurses5-devel ncurses-devel wget  git cmake openssl gcc-c++ gcc  openssl-devel MySQL-python  -y

注意:centos7 系统自带的bison的版本过高,在后面测试的时候会报错!
查看版本: bison -V
下载地址: wget http://ftp.gnu.org/gnu/bison/bison-2.5.1.tar.gz

cd  bison-2.5.1
./configure
make && make install

安装完成后:bison -V

 

2、安装inception

git clone https://github.com/cookieY/Yearning.git
cd /opt/
git clone https://github.com/mysql-inception/inception
注意:centos6.5可能git clone报错,升级git版本,升级:依赖yum update nss,就正常了:
升级参考:https://blog.csdn.net/zzpzheng/article/details/52132385
cd inception
sh inception_build.sh debug
debug就是编译目录,编译后所有的生成文件都在这个目录下面,包括可执行文件Inception。可执行文件在debug/sql目录下面:
/opt/inception/debug/sql
[root@mysql sql]# ls
CMakeFiles           dummy.bak     Inception   libbinlog.a  librpl.a    libsql.a          Makefile  sql_builtin.cc  sql_yacc.h
cmake_install.cmake  gen_lex_hash  lex_hash.h  libmaster.a  libslave.a  libsqlgunitlib.a  share     sql_yacc.cc

3、创建配置目录,将可执行文件Inception拷贝到/opt/inception/bin目录

[root@mysql sql]# mkdir -p /data/inception
[root@mysql sql]# mkdir -p /data/inception/bin
[root@mysql sql]# mkdir -p /data/inception/temp
[root@mysql sql]# cp Inception /data/inception/bin/

编辑配置文件:
[root@mysql sql]# cat /etc/inc.cnf
[inception]
general_log=1
general_log_file=/opt/inception/inception.log
port=6669
socket=/opt/inception/inc.socket
character-set-client-handshake=0
character-set-server=utf8
inception_remote_system_password=123456
inception_remote_system_user=root
inception_remote_backup_port=3306
inception_remote_backup_host=127.0.0.1
inception_support_charset=utf8mb4
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_bin_dir=/opt/inception/temp
inception_osc_chunk_time=0.1
inception_ddl_support=1
inception_enable_blob_type=1
inception_check_column_default_value=1

启动

[root@mysql sql]# nohup /data/inception/bin/Inception --defaults-file=/etc/inc.cnf &
[1] 2398
[root@mysql sql]# nohup: ignoring input and appending output to `nohup.out'
[root@mysql sql]# ps -ef|grep Inception
root      2414 23520  0 23:15 pts/0    00:00:00 /opt/inception/bin/Inception --defaults-file=/etc/inc.cnf
root      2419 23520  0 23:22 pts/0    00:00:00 grep Inception


登录验证

启动数据库,没有的可以自行安装。

# mysql -uroot -h127.0.0.1 -P6669
-bash: mysql: 未找到命令

注:没有安装MYSQL 数据库如下:

安装mysql:
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install mysql-server
systemctl start mysql

[root@mysql sql]# mysql -uroot -h127.0.0.1 -P6669
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: Inception2.1.50 1
Copyright (c) 2009-2014 Percona LLC and/or its affiliates
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> inception get variables;
。。。。。
| version                                  | Inception2.1.50                                 |
| version_comment                          | Source distribution                             |
| version_compile_machine                  | x86_64                                          |
| version_compile_os                       | Linux                                           |
| wait_timeout                             | 28800                                           |
+------------------------------------------+-------------------------------------------------+
rows in set (0.00 sec)
mysql> exit
Bye
[root@mysql sql]#

 

 

二、web界面,安装Yearning

安装环境:
python3.6 + mysql5.6 +nginx

安装python3.6
 wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz
tar xvf Python-3.6.3.tgz  
mkdir /usr/local/python3
cd Python-3.6.3/  
./configure --prefix=/usr/local/python3  --with-ssl  && make && make install

将/opt/python3/bin加入环境变量

 

##########将/opt/python3/bin加入环境变量#########

 

echo 'export PATH=$PATH:/opt/python3.2.3/bin/' >> ~/.bashrc
.  ~/.bashrc
加入我们需要用它来代替系统的默认的 python,即运行 python就是运行 python3,而且要是 yum可以正常使用,我们可以执行如下步骤:

mv /usr/bin/python /usr/bin/python-old-backup

ln -s /opt/python3.2.3/bin/python3 /usr/bin/python

(ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3)

vim /usr/bin/yum
#将文件开头的
#!/usr/bin/python 修改为 #/usr/bin//usr/bin/python-old-backup

###############################

yum 出现 except OSError, e: ^ SyntaxError: invalid syntax

错误原因:

       系统中装有多个版本的Python,Python脚本运行的时候版本冲突。

     解决办法:(以下两步都要执行)

    1.  vim /usr/libexec/urlgrabber-ext-down
        将/usr/bin/python改为/usr/bin/python2.7。
    2. vim /usr/bin/yum-config-manager
      解决办法同上: #!/usr/bin/python换成 #!/usr/bin/python2.7 

 

 

安装mysql:
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install mysql-server
systemctl start mysql

进入mysql
创建数据库并授权

CREATE DATABASE IF NOT EXISTS Yearning default charset utf8 COLLATE utf8_general_ci;
grant all on *.* to 'yearning'@'%' identified by 'yearning';

下载Yearning源码:
git clone https://github.com/cookieY/Yearning.git
cd /opt/Yearning/src
cp deploy.conf.template deploy.conf

更改配置文件:
[root@localhost src]# cat deploy.conf
[mysql]
db = Yearning
address = 127.0.0.1
port = 3306
username =yearning
password =yearning

[host]
ipaddress = 192.168.1.39:8080

[Inception]
ip = 127.0.0.1
port = 6669
user = root
password  = x
backupdb = 127.0.0.1
backupport = 3306
backupuser = root
backuppassword =

[LDAP]
LDAP_SERVER =
LDAP_SCBASE =
LDAP_DOMAIN =
LDAP_TYPE = 1

[email]
username =
password =
smtp_server =
smtp_port = 25

[sql]

 

 

limit = 1000

[webhook]
dingding =

 

 

 

 

#####编辑 Yearning/src/deploy.conf##########

 

  1. [mysql]
  2. db = 所创建的库名
  3. address = 数据库地址
  4. port = 数据库端口
  5. password = 数据库密码
  6. username = 数据库用户
  7. [host]
  8. ipaddress = 服务器ip地址:端口 (涉及跨域十分重要!!设置不正确将无法登陆!!)
  9. 例如 本机地址为192.168.1.2 nginx端口为80(端口可自己定义,定义什么端口就填写什么端口)
  10. 则应填写为 192.168.1.2:80 之后只能通过该地址访问平台其他地址一概无效!
  11. 注意!访问的是nginx的端口不是后端8000端口!
  12. (仅对于v1.2版本以下版本,对于1.2以上版本此配置信息只用来消息推送时显示平台地址)
  13. 特别注意! 以下配置信息为v1.2版本以下配置信息。v1.2版本及以上版本不存在以下配置信息
  14. [Inception]
  15. ip = Inception地址
  16. port = Inception端口
  17. user = Inception用户名
  18. password = Inception密码
  19. backupdb = 备份数据库地址
  20. backupport = 备份数据库端口
  21. backupuser = 备份数据库用户名
  22. backuppassword = 备份数据库密码
  23. [LDAP] LDAP相关设置
  24. LDAP_SERVER = LDAP服务地址
  25. LDAP_SCBASE = LDAP dc 设置 如 dc=xxx,dc=com
  26. LDAP_DOMAIN = LDAP域名 如 xxx.com
  27. LDAP_TYPE = 1 1 通过域名进行ldap认证 2 通过uid进行ldap认证 3 通过cn进行ldap认证
  28. [email] 邮箱推送相关设置
  29. username = 邮箱发件账号 如 xxxx@163.com
  30. password = 邮箱发件账号密码
  31. smtp_server = 邮箱stmp地址, 具体地址请咨询对应邮箱提供者
  32. [sql]
  33. limit = 100 查询最高的Limit数量
  34. [webhook]
  35. dingding = 权限提交,查询工单等其他非sql工单提交消息推送钉钉webhook

 

###################

 

 

安装相应python依赖库
/usr/local/python3/bin/pip3 install -r requirements.txt

初始化数据库
python3 manage.py makemigrations core && python3 manage.py migrate core

添加初始化用户
echo "from core.models import Account;Account.objects.create_user(username='admin', password='Jlkj#123', group='admin',is_staff=1)" | python3 manage.py shell


初始化权限
echo "from core.models import grained;grained.objects.get_or_create(username='admin', permissions={'person': [], 'ddl': '1', 'ddlcon': [], 'dml': '1', 'dmlcon': [], 'dic': '1', 'diccon': [], 'dicedit': '0', 'user': '1', 'base': '1', 'dicexport': '0'})" | python3 manage.py shell


复制编译好的静态文件到nginx html目录下(如自行更改Nginx静态路径地址则将静态文件复制到对应静态文件目录下)
安装ningx
yum install -y nginx
######################

1.添加Nginx到YUM源

添加CentOS 7 Nginx yum资源库,打开终端,使用以下命令:

sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

2.安装Nginx

在你的CentOS 7 服务器中使用yum命令从Nginx源服务器中获取来安装Nginx:

sudo yum install -y nginx

Nginx将完成安装在你的CentOS 7 服务器中。

3.启动Nginx

刚安装的Nginx不会自行启动。运行Nginx:

sudo systemctl start nginx.service

如果一切进展顺利的话,现在你可以通过你的域名或IP来访问你的Web页面来预览一下Nginx的默认页面;

nginx_default

如果看到这个页面,那么说明你的CentOS 7 中 web服务器已经正确安装。

CentOS 7 开机启动Nginx

sudo systemctl enable nginx.service

更多systemctl命令可查看《systemctl命令用法

Nginx配置信息

网站文件存放默认目录

/usr/share/nginx/html

网站默认站点配置

/etc/nginx/conf.d/default.conf

自定义Nginx站点配置文件存放目录

/etc/nginx/conf.d/

Nginx全局配置

/etc/nginx/nginx.conf

 

Nginx启动

nginx -c nginx.conf

 

在这里你可以改变设置用户运行Nginx守护程序进程一样,和工作进程的数量得到了Nginx正在运行,等等。

Linux查看公网IP

您可以运行以下命令来显示你的服务器的公共IP地址:

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

 

或者

最后安装nginx
下载Nginx: wget http://nginx.org/download/nginx-1.3.11.tar.gz /opt/
  ./configure && make && make install
 启动nginx
启动:/usr/local/nginx/sbin/nginx
重启:kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
停止nginx:pkill -9 nginx

##########################


启动nginx

systemctl start nginx  
cp -rf Yearning/webpage/dist/* $NGINX_HOME/html/

启动django
python3 manage.py runserver 0.0.0.0:8000
注:

是否受防火墙安全控制等。

将iptables和selinux关闭
以下4条命令清除iptables的配置

  1. iptables -F
  2. iptables -F -t nat
  3. iptables -X
  4. iptables -X -t nat

setenforce 0 #关闭selinux

访问首页http://IP

 

此时使用我们初始化的账号密码:admin  Jlkj#123 登录即可

 

注:添加初始化用户,登录后如下图所示(如果登录不了,注意看#2.9是否增加了8000的安全组策略)

2.9、修改阿里云的安全组策略(解决不能用公网IP访问的问题,请一定注意增加80和8000两个端口,否则还是登录不了),参考以下文章
http://blog.csdn.net/LJFPHP/article/details/78670459

 

 

 

 

三、archer安装

开发语言和推荐环境

python:3.4

 

wget https://www.python.org/ftp/python/3.4.1/Python-3.4.1.tgz

django:1.8
mysql : 5.6及以上
linux : 64位linux操作系统均可

环境准备

1、克隆代码到本地: git clone https://github.com/jly8866/archer.git 或 下载zip包

git clone -b archer-2.0 https://github.com/jly8866/archer.git

详细说明文档:https://gitee.com/ilanni/archer

2、安装mysql 5.6实例,请注意保证mysql数据库默认字符集为utf8或utf8mb4
3、安装inception

1、安装所需软件和模块

1)安装python3 (前面有)

2)安装setuptools

 

[root@mysql3 ~]# wget --no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-19.6.tar.gz#md5=c607dd118eae682c44ed146367a17e26

[root@mysql3 ~]# tar -zxvf setuptools-19.6.tar.gz

[root@mysql3 ~]# cd setuptools-19.6

[root@mysql3 setuptools-19.6]# python3 setup.py build

[root@mysql3 setuptools-19.6]# python3 setup.py install

... ...

creating 'dist/setuptools-19.6-py3.4.egg' and adding 'build/bdist.linux-x86_64/egg' to it

removing 'build/bdist.linux-x86_64/egg' (and everything under it)

Processing setuptools-19.6-py3.4.egg

Copying setuptools-19.6-py3.4.egg to /opt/python3/lib/python3.4/site-packages

Adding setuptools 19.6 to easy-install.pth file

Installing easy_install script to /opt/python3/bin

Installing easy_install-3.4 script to /opt/python3/bin

Installed /opt/python3/lib/python3.4/site-packages/setuptools-19.6-py3.4.egg

Processing dependencies for setuptools==19.6

Finished processing dependencies for setuptools==19.6

可以看到安装到python3下面了

3)安装pip

 

[root@mysql3 ~]# wget --no-check-certificate https://pypi.python.org/packages/source/p/pip/pip-8.0.2.tar.gz#md5=3a73c4188f8dbad6a1e6f6d44d117eeb

[root@mysql3 ~]# tar -zxvf pip-8.0.2.tar.gz

[root@mysql3 ~]# cd pip-8.0.2

[root@mysql3 pip-8.0.2]# python3 setup.py build

[root@mysql3 pip-8.0.2]# python3 setup.py install

... ...

creating 'dist/pip-8.0.2-py3.4.egg' and adding 'build/bdist.linux-x86_64/egg' to it

removing 'build/bdist.linux-x86_64/egg' (and everything under it)

Processing pip-8.0.2-py3.4.egg

creating /opt/python3/lib/python3.4/site-packages/pip-8.0.2-py3.4.egg

Extracting pip-8.0.2-py3.4.egg to /opt/python3/lib/python3.4/site-packages

Adding pip 8.0.2 to easy-install.pth file

Installing pip3 script to /opt/python3/bin

Installing pip3.4 script to /opt/python3/bin

Installing pip script to /opt/python3/bin

Installed /opt/python3/lib/python3.4/site-packages/pip-8.0.2-py3.4.egg

Processing dependencies for pip==8.0.2

Finished processing dependencies for pip==8.0.2

4)安装Django

使用pip3安装:pip3 install Django==1.8.17

wget  http://mirrors.aliyun.com/pypi/packages/58/4a/26f99e2b094a2edefb2cffbcdbaca9207835d4b2765dd8afa553a1714ea6/Django-2.0.7.tar.gz

 

或者单独下载安装:

 

[root@mysql3 ~]# wget https://pypi.python.org/packages/44/41/bf93934082e9897a56a591a67bacbd9fb74e71244f3f42253432a9e627e6/Django-1.8.17.tar.gz#md5=e76842cdfbcb31286bd44f51e087a04c

[root@mysql3 ~]# tar -zxvf Django-1.8.17.tar.gz

[root@mysql3 ~]# cd Django-1.8.17

[root@mysql3 Django-1.8.17]# python3 setup.py install

[root@mysql3 Django-1.8.17]# python3

Python 3.4.1 (default, Apr 15 2017, 14:11:11)

[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> django.VERSION

(1, 8, 17, 'final', 0)

>>> exit()

注1:Django支持python版本对照表

Django版本

Python版本

1.5.x

2.6.5

2.7

3.2

3.3

1.6.x

2.6

2.7

3.2

3.3

1.7.x

2.7

3.2

3.3

3.4

1.8.x

2.7

3.2

3.3

3.4

3.5

LTS

1.9.x

2.7

3.4

3.5

1.10.x

2.7

3.4

3.5

1.11.x

开发推荐

2.7

3.4

3.5

3.6

LTS

2.0.x

3.4

3.5

3.6

注1:查看python版本和django版本

python --version

在python shell中:

  import sys

  sys.version

  import django

  django.VERSION

 

5)安装GIT

yum -y install git

6)安装Crypto和pymysql

[root@mysql3 ~]# pip3 install Crypto
[root@mysql3 ~]# pip3 install pycrypto

[root@mysql3 ~]# pip3 install pymysql

 

[root@mysql3 ~]# vi /opt/python3/lib/python3.4/site-packages/pymysql/connections.py
在if int(self.server_version.split(‘.’, 1)[0]) >= 5: 这一行之前加上以下这一句并保存,记得别用tab键用4个空格缩进:
self.server_version = ‘5.6.24-72.2-log’
修改后如下图:
图1

2、配置archer

1、将archer-master源码解压到/opt/archer目录

 

[root@mysql3 archer-master]# mkdir -p /opt/archer

[root@mysql3 archer-master]# scp -r /src/archer-master /opt/archer/

[root@mysql3 archer-master]# cd /opt/archer/

[root@mysql3 archer]# ls

archer debug.sh docs manage.py README.md requirements.txt screenshots sql startup.sh stop.sh

2、创建archer DB和inception DB

 

create database archer;(CREATE DATABASE 数据库名 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;)

grant all on archer.* to archer_rw@'%' identified by 'archer_rw';
 create database inception_db;
 grant all on inception_db.* to incep_rw@'%' identified by 'incep_rw';
flush privileges;

注:设置数据库字符集

1.alter database test default character set = utf8;

2.create database mydb character set utf8;

修改单个表的字符集

1.alter table test1 default character set = utf8;

2.alter table test2 convert to character set utf8;

使用MySQL字符集的建议:建立数据库/表和进行数据库操作时尽量显式指出使用的字符集,而不是依赖于MySQL的默认设置;

或者https://blog.csdn.net/eagle89/article/details/80776741

 

3、修改/opt/archer/archer/settings.py,如下内容:

 

#该项目本身的mysql数据库地址

DATABASES = {

'default': {

'ENGINE': 'django.db.backends.mysql',

'NAME': 'archer',

'USER': 'archer_rw',

'PASSWORD': 'archer_rw',

'HOST': '192.168.1.3',

'PORT': '3306'

}

}

#inception组件所在的地址

INCEPTION_HOST = '192.168.1.123'

INCEPTION_PORT = '6669'

#查看回滚SQL时候会用到,这里要告诉archer去哪个mysql里读取inception备份的回滚信息和SQL.

#注意这里要和inception组件的inception.conf里的inception_remote_XX部分保持一致.

INCEPTION_REMOTE_BACKUP_HOST='192.168.1.3'

INCEPTION_REMOTE_BACKUP_PORT=3306

INCEPTION_REMOTE_BACKUP_USER='incep_rw'

INCEPTION_REMOTE_BACKUP_PASSWORD='incep_rw'

4、通过model创建archer本身的数据库表

总结:

 

  1. pip3 install django_admin_bootstrapped
  2. python3 manage.py makemigrations
  3. python3 manage.py makemigrations sql
  4. python3 manage.py migrate

######django models error#######

 

问题1.将主键id改成自动生成的python3 manage.py migrate时报下面的错误

django.db.utils.InternalError: (1091, "Can‘t DROP ‘id‘; check that column/key exists")

 

我猜测是因为我改了两个表的主键,其中A表主键是B表的外键,删除会有问题

 

解决方法:

1.删除app下migrations文件夹中000开头的记录文件

2.删掉app对应的数据库drop database 数据库名;

4.重新建立刚刚删除的数据库create database 数据库名;

3.然后重新执行修改数据库的命令:

  python3 manage.py makemigrations

  python3 manage.py migrate

 

问题2:django向数据库中添加中文时报错

django.db.utils.InternalError: (1366, "Incorrect string value: ‘\\xE7\\x99\\xBB\\xE5\\xBD\\x95...‘ for column ‘chinessname‘ at row 1")

 

解决方法:创建数据库的时候设置编码格式

 CREATE DATABASE 数据库名 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

 

问题3:增加主键没生效,每次都只是执行了update

django的models通过AutoField设置自增主键后,没有递增,代码如下

class elements(models.Model):
    elementid=models.AutoField(primary_key=True,default=1)

    pageid=models.ForeignKey(page)

 

解决方法:去掉default=1

如果有default,它实现的是update,只有去掉default,才执行insert

 

问题4:models存储包含外键的数据时,报错

ValueError: Cannot assign "‘12‘": "elements.pageid" must be a "page" instance.

 

models的数据结构:

class page(models.Model):
    pageid=models.AutoField(primary_key=True)

    

class elements(models.Model):
    elementid=models.AutoField(primary_key=True)

    pageid=models.ForeignKey(page)

 

解决方法:

page对象的paged是elements的外键,在使用save时,需要传递一个page对象实例

elements(pageid=page.objects.get(pageid=xxx).save()

 

###############

 

 

 

[root@mysql3]# cd /opt/archer

[root@mysql3 archer]# python3 manage.py makemigrations

No changes detected

[root@mysql3 archer]# python3 manage.py makemigrations sql

Migrations for 'sql':

0001_initial.py:

- Create model users

- Create model master_config

- Create model workflow

[root@mysql3 archer]# python3 manage.py migrate

Operations to perform:

Synchronize unmigrated apps: messages, staticfiles

Apply all migrations: sessions, auth, contenttypes, sql, admin

Synchronizing apps without migrations:

Creating tables...

Running deferred SQL...

Installing custom SQL...

Running migrations:

Rendering model states... DONE

Applying contenttypes.0001_initial... OK

Applying contenttypes.0002_remove_content_type_name... OK

Applying auth.0001_initial... OK

Applying auth.0002_alter_permission_name_max_length... OK

Applying auth.0003_alter_user_email_max_length... OK

Applying auth.0004_alter_user_username_opts... OK

Applying auth.0005_alter_user_last_login_null... OK

Applying auth.0006_require_contenttypes_0002... OK

Applying sql.0001_initial... OK

Applying admin.0001_initial... OK

Applying sessions.0001_initial... OK

查看表是否创建成功:

[root@mysql3 archer]# mysql -uarcher_rw -parcher_rw -h192.168.1.3 -P3306 -Darcher -e "show tables;"

Warning: Using a password on the command line interface can be insecure.

+----------------------------+

| Tables_in_archer |

+----------------------------+

| auth_group |

| auth_group_permissions |

| auth_permission |

| django_admin_log |

| django_content_type |

| django_migrations |

| django_session |

| sql_master_config |

| sql_users |

| sql_users_groups |

| sql_users_user_permissions |

| sql_workflow |

+----------------------------+

5、创建django admin管理员

 

[root@mysql3 archer]# python3 manage.py createsuperuser

Username: admin

Email address: river_dba@foxmail.com

Password: admin123

Password (again): admin123

Superuser created successfully.

该用户可以登录django admin来管理model。

启动acher

 

[root@mysql3 archer]# vi debug.sh

#!/bin/bash

python3 manage.py runserver 0.0.0.0:9123

[root@mysql3 archer]# sh debug.sh

Performing system checks...

System check identified no issues (0 silenced).

April 15, 2017 - 16:39:11

Django version 1.8.17, using settings 'archer.settings'

Starting development server at http://0.0.0.0:9123/

Quit the server with CONTROL-C.

创建archer系统登录用户

通过浏览器访问http://192.168.1.123:9123/admin/sql/users/可以看到django登录界面:图2使用上面第5步创建的用户名密码(admin/admin123)登录:图3点击右侧Add用户配置,用户名密码自定义,至少创建一个工程师和一个审核人(用admin用户可以登录)后续新的工程师和审核人用户请用LDAP导入sql_users表或django admin增加图4

配置主库地址

通过浏览器访问http://192.168.1.123:9123/admin/sql/master_config点击右侧Add master_config。这一步是为了告诉archer你要用inception去哪些mysql主库里执行SQL,所用到的用户名密码、端口等。图5图6

正式访问主页

http://192.168.1.123:9123/login/图7登录成功后:图8

遇到问题及解决

查看回滚SQL时报错

图9debug日志报错:

 

[18/Apr/2017 11:49:50] "GET /detail/2/ HTTP/1.1" 200 7474

Mysql Error 1142: SELECT command denied to user 'incep_rw'@'192.168.1.123' for table '$_$inception_backup_information$_$'

Error: returned listTables more than 1.

[18/Apr/2017 11:50:01] "GET /rollback/?csrfmiddlewaretoken=8s6iaPsMtgX1Qvd0lf8vDOXe0K9hPecp&workflowid=2 HTTP/1.1" 500 73965

解决办法:给incep_rw用户赋予select、create、insert权限

 

root@mysql 04:42:04>grant select,create,insert on *.* to incep_rw@'%' identified by 'incep_rw';

Query OK, 0 rows affected (0.00 sec)

root@mysql 04:54:23>flush privileges;

Query OK, 0 rows affected (0.00 sec)

CREATE权限用于创建表或者库的,INSERT权限用于插入备份数据的,SELECT权限用于查询回滚SQL。再次执行update语句,查看回滚SQL为空,审核结果显示:图9解决办法:为mysql添加server_id

 

在线查询&脱敏查询

  1. settings中QUERY改为True
  2. 到【后台数据管理】-【从库地址配置】页面添加从库信息
  3. 到【后台数据管理】-【工作流配置】页面配置审核流程
  4. 用户申请权限、审核通过后即可进行在线查询
  5. 如需要使用动态脱敏,请将settings中DATA_MASKING_ON_OFF改为True,并且到【后台数据管理】-【脱敏配置】页面配置脱敏规则和字段

慢日志管理

  1. settings中SLOWQUERY改为True

  2. 安装percona-toolkit(版本>3.0),以centos为例

    1. yum -y install http://www.percona.com/downloads/percona-release/redhat/0.1-3/percona-release-0.1-3.noarch.rpm
    2. yum -y install percona-toolkit.x86_64
    install http://www.percona.com/downloads/percona-release/redhat/0.1-3/percona-release-0.1-3.noarch.rpm yum -y install percona-toolkit.x86_64
  3. 使用src/script/mysql_slow_query_review.sql创建慢日志收集表到archer数据库

  4. 将src/script/analysis_slow_query.sh部署到各个监控机器,注意修改脚本里面的 hostname="${mysql_host}:${mysql_port}"与archer主库配置信息一致,否则将无法筛选到相关记录

 

阿里云rds管理

  1. settings修改配置文件ALIYUN_RDS_MANAGE=True
  2. 安装模块
  1. pip3 install aliyun-python-sdk-core==2.3.5
  2. pip3 install aliyun-python-sdk-core-v3==2.5.3
  3. pip3 install aliyun-python-sdk-rds==2.1.1
install aliyun-python-sdk-core==2.3.5 pip3 install aliyun-python-sdk-core-v3==2.5.3 pip3 install aliyun-python-sdk-rds==2.1.1
  1. 在【后台数据管理】-【阿里云认证信息】页面,添加阿里云账号的accesskey信息,重新启动服务
  2. 在【后台数据管理】-【阿里云rds配置】页面,添加实例信息,即可实现对阿里云rds的进程管理、慢日志管理

admin后台加固,防暴力破解

  1. patch目录下,名称为:django_1.8.17_admin_secure_archer.patch
  2. 使用命令:
patch  python/site-packages/django/contrib/auth/views.py django_1.8.17_admin_secure_archer.patch
.py django_1.8.17_admin_secure_archer.patch

集成ldap

  1. 修改配置文件ENABLE_LDAP=True,安装相关模块,可以启用ldap账号登陆,以centos为例
  1. yum install openldap-devel
  2. pip install django-auth-ldap==1.3.0
install openldap-devel pip install django-auth-ldap==1.3.0
  1. 如果使用了ldaps,并且是自签名证书,需要打开settings中AUTH_LDAP_GLOBAL_OPTIONS的注释
  2. settings中以AUTH_LDAP开头的配置,需要根据自己的ldap对应修改

部分功能使用说明

  1. 用户角色配置
    在【后台数据管理】-【用户配置】页面管理用户,或者使用LADP导入,至少拥有一个工程师角色(engineer)、一个DBA角色才可以进行SQL上线工程师可以发起SQL上线,审核人/DBA进行审核,DBA进行执行,超级管理员可以登录admin界面进行管理
  2. 配置主库地址
    在【后台数据管理】-【主库地址配置】页面管理主库
    主库地址用于SQL上线,DDL、DML、慢日志查看、SQL优化等功能
  3. 配置从库地址
    在【后台数据管理】-【从库地址配置】页面管理从库
    从库地址用于SQL查询功能
  4. 配置查询权限审核流程
    在【后台数据管理】-【工作流配置】页面管理审核流程

 

  1. archer安装部署
  2. ***************************************************************************
  3. --版本
  4. python:3.4
  5. django:1.8
  6. mysql : 5.6以上
  7. linux : 64位linux
  8. --安装archer
  9. mkdir -p /opt/archer_web/
  10. cd /opt/archer_web
  11. yum -y install git
  12. git clone https://github.com/jly8866/archer.git
  13. --安装python3
  14. cd /opt
  15. wget https://www.python.org/ftp/python/3.4.1/Python-3.4.1.tgz
  16. tar -zxvf Python-3.4.1.tgz
  17. cd Python-3.4.1
  18. ./configure --prefix=/opt/python3 && make && make install
  19. vim /etc/profile
  20. 添加:export PATH="/opt/python3/bin:$PATH"
  21. source /etc/profile
  22. python3
  23. --安装setuptools
  24. cd /opt
  25. wget --no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-19.6.tar.gz#md5=c607dd118eae682c44ed146367a17e26
  26. tar -zxvf setuptools-19.6.tar.gz
  27. cd setuptools-19.6
  28. python3 setup.py build
  29. python3 setup.py install
  30. 可以看到安装到python3下面了
  31. --安装pip
  32. cd /opt
  33. wget --no-check-certificate https://pypi.python.org/packages/source/p/pip/pip-8.0.2.tar.gz#md5=3a73c4188f8dbad6a1e6f6d44d117eeb
  34. tar -zxvf pip-8.0.2.tar.gz
  35. cd pip-8.0.2
  36. python3 setup.py build
  37. python3 setup.py install
  38. --安装Django
  39. cd /opt
  40. wget https://pypi.python.org/packages/44/41/bf93934082e9897a56a591a67bacbd9fb74e71244f3f42253432a9e627e6/Django-1.8.17.tar.gz#md5=e76842cdfbcb31286bd44f51e087a04c
  41. tar -zxvf Django-1.8.17.tar.gz
  42. cd Django-1.8.17
  43. python3 setup.py install
  44. python3
  45. >>> import django
  46. >>> django.VERSION
  47. (1, 8, 17, 'final', 0)
  48. >>> exit()
  49. --安装Crypto和pymysql
  50. pip3 install Crypto
  51. pip3 install pycrypto
  52. pip3 install pymysql
  53. 这里遇到了错误
  54. # pip3 install Crypto
  55. -bash: ./pip3: /usr/bin/python3: bad interpreter: No such file or directory
  56. 解决方法:
  57. python3.4 -m pip install Crypto
  58. 或者
  59. vim /opt/python3/bin/pip3
  60. 将#!/usr/bin/python3改为#!/opt/python3/bin/python3.4
  61. # vim /opt/python3/lib/python3.4/site-packages/pymysql/connections.py
  62. 在if int(self.server_version.split(‘.’, 1)[0]) >= 5: 这一行之前加上以下这一句并保存,记得别用tab键用4个空格缩进:
  63. self.server_version = '5.6.24-72.2-log'
  64. --配置archer
  65. cd /opt/archer_web/archer
  66. --创建archer DB和inception DB
  67. --进入安装的mysql5.6
  68. mysql -uroot -p'P@ssw0rd' -S /mydata/mysql/mysql3306/mysql.sock
  69. --grant all privileges on *.* to root@'%' identified by 'P@ssw0rd';
  70. create database archer default character set utf8;
  71. grant all on archer.* to archer_rw@'%' identified by 'P@ssw0rd';
  72. create database inception_db default character set utf8;
  73. grant all on inception_db.* to incep_rw@'%' identified by 'P@ssw0rd';
  74. grant select,create,insert on *.* to incep_rw@'%' identified by 'P@ssw0rd';
  75. create database test default character set utf8; #测试库 建在审核库里了 仅做测试
  76. grant all on test.* to test@'%' identified by 'P@ssw0rd';
  77. flush privileges;
  78. ########创建archer DB和inception DB#############
  79.  create database archer;
  80. grant all on archer.* to archer_rw@'%' identified by 'archer_rw';
  81. create database inception_db;
  82. grant all on inception_db.* to incep_rw@'%' identified by 'incep_rw';
  83. flush privileges;
  84. ########################
  85.  --修改/opt/archer_web/archer/archer/settings.py,如下内容:
  86. vi /opt/archer_web/archer/archer/settings.py
  87. #该项目本身的mysql数据库地址
  88. DATABASES = {
  89. 'default': {
  90. 'ENGINE': 'django.db.backends.mysql',
  91. 'NAME': 'archer',
  92. 'USER': 'archer_rw',
  93. 'PASSWORD': 'P@ssw0rd',
  94. 'HOST': '127.0.0.1',
  95. 'PORT': '3306'
  96. }
  97. }
  98. #inception组件所在的地址
  99. INCEPTION_HOST = '10.10.3.70'
  100. INCEPTION_PORT = '6669'
  101. #查看回滚SQL时候会用到,这里要告诉archer去哪个mysql里读取inception备份的回滚信息和SQL.
  102. #注意这里要和inception组件的inception.conf里的inception_remote_XX部分保持一致.
  103. INCEPTION_REMOTE_BACKUP_HOST='10.10.3.70'
  104. INCEPTION_REMOTE_BACKUP_PORT=3306
  105. INCEPTION_REMOTE_BACKUP_USER='incep_rw'
  106. INCEPTION_REMOTE_BACKUP_PASSWORD='P@ssw0rd' 这个用户就是在部署inception使用的用户。(这用户应该是和上面创建的incep_rw是一个用户,可以不用创建incep_rw)
  107. --通过model创建archer本身的数据库表
  108. cd /opt/archer_web/archer
  109. pip3 install django_admin_bootstrapped
  110. python3 manage.py makemigrations
  111. python3 manage.py makemigrations sql
  112. python3 manage.py migrate
  113. 进入数据库查看archer库下的表是否存在
  114. ##########创建archer本身的数据库表#####################
  115. [root@192-168-1-34 archer]# python3 manage.py makemigrations
  116. No changes detected
  117. [root@192-168-1-34 archer]# python3 manage.py makemigrations sql
  118. No changes detected in app 'sql'
  119. [root@192-168-1-34 archer]# python3 manage.py migrate
  120. Operations to perform:
  121.   Synchronize unmigrated apps: messages, django_admin_bootstrapped, staticfiles
  122.   Apply all migrations: admin, sessions, auth, sql, django_apscheduler, contenttypes
  123. Synchronizing apps without migrations:
  124.   Creating tables...
  125.     Running deferred SQL...
  126.   Installing custom SQL...
  127. Running migrations:
  128.   Rendering model states... DONE
  129.   Applying contenttypes.0001_initial... OK
  130.   Applying contenttypes.0002_remove_content_type_name... OK
  131.   Applying auth.0001_initial... OK
  132.   Applying auth.0002_alter_permission_name_max_length... OK
  133.   Applying auth.0003_alter_user_email_max_length... OK
  134.   Applying auth.0004_alter_user_username_opts... OK
  135.   Applying auth.0005_alter_user_last_login_null... OK
  136.   Applying auth.0006_require_contenttypes_0002... OK
  137.   Applying sql.0001_initial... OK
  138.   Applying admin.0001_initial... OK
  139.   Applying django_apscheduler.0001_initial... OK
  140.   Applying django_apscheduler.0002_auto_20180412_0758... OK
  141.   Applying sessions.0001_initial... OK
  142. [root@192-168-1-34 archer]# mysql
  143. Welcome to the MySQL monitor.  Commands end with ; or \g.
  144. Your MySQL connection id is 9
  145. Server version: 5.6.40 MySQL Community Server (GPL)
  146. Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  147. Oracle is a registered trademark of Oracle Corporation and/or its
  148. affiliates. Other names may be trademarks of their respective
  149. owners.
  150. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  151. mysql> show databases;
  152. +--------------------+
  153. | Database           |
  154. +--------------------+
  155. | information_schema |
  156. | archer             |
  157. | inception_db       |
  158. | mysql              |
  159. | performance_schema |
  160. | test               |
  161. +--------------------+
  162. 6 rows in set (0.00 sec)
  163. mysql> use archer;
  164. Reading table information for completion of table and column names
  165. You can turn off this feature to get a quicker startup with -A
  166. Database changed
  167. mysql> show tables;
  168. +---------------------------------------+
  169. | Tables_in_archer                      |
  170. +---------------------------------------+
  171. | aliyun_access_key                     |
  172. | aliyun_rds_config                     |
  173. | auth_group                            |
  174. | auth_group_permissions                |
  175. | auth_permission                       |
  176. | data_masking_columns                  |
  177. | data_masking_rules                    |
  178. | django_admin_log                      |
  179. | django_apscheduler_djangojob          |
  180. | django_apscheduler_djangojobexecution |
  181. | django_content_type                   |
  182. | django_migrations                     |
  183. | django_session                        |
  184. | query_log                             |
  185. | query_privileges                      |
  186. | query_privileges_apply                |
  187. | sql_master_config                     |
  188. | sql_slave_config                      |
  189. | sql_users                             |
  190. | sql_users_groups                      |
  191. | sql_users_user_permissions            |
  192. | sql_workflow                          |
  193. | workflow_audit                        |
  194. | workflow_audit_detail                 |
  195. | workflow_audit_setting                |
  196. +---------------------------------------+
  197. 25 rows in set (0.00 sec)
  198. mysql> show create table aliyun_rds_config;
  199. +-------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  200. | Table             | Create Table                                                                                                                                                                                                                                                          |
  201. +-------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  202. | aliyun_rds_config | CREATE TABLE `aliyun_rds_config` (
  203.   `id` int(11) NOT NULL AUTO_INCREMENT,
  204.   `rds_dbinstanceid` varchar(100) NOT NULL,
  205.   `cluster_name` varchar(50) NOT NULL,
  206.   PRIMARY KEY (`id`),
  207.   UNIQUE KEY `cluster_name` (`cluster_name`)
  208. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
  209. +-------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  210. 1 row in set (0.00 sec)
  211. mysql> exit
  212. Bye
  213. [root@192-168-1-34 archer]# python3 manage.py createsuperuser
  214. Username: admin
  215. Email address: ddd@sina.com
  216. Password:
  217. Password (again):
  218. Superuser created successfully.
  219. [root@192-168-1-34 archer]# ls
  220. archer  debug.bat  debug.sh  LICENSE  manage.py  patch  README.md  requirements.txt  sql  src  startup.sh  stop.sh
  221. [root@192-168-1-34 archer]# sh debug.sh
  222. Performing system checks...
  223. System check identified no issues (0 silenced).
  224. June 21, 2018 - 09:49:24
  225. Django version 1.8.17, using settings 'archer.settings'
  226. Starting development server at http://192.168.1.34:9123/
  227. Quit the server with CONTROL-C.
  228. #######################################
  229.  --创建django admin管理员
  230. python3 manage.py createsuperuser
  231. Username: admin 自己填写用户名
  232. Email address: 邮箱
  233. Password: 密码
  234. Password (again): 确认密码
  235. Superuser created successfully.
  236. 该用户可以登录django admin来管理model。
  237. --启动acher
  238. vi debug.sh
  239. #!/bin/bash
  240. python3 manage.py runserver ip:9123 端口为9123
  241. nohup sh debug.sh 2&>1 &
  242. [root@192-168-1-34 archer]# sh debug.sh
  243. Performing system checks...
  244. System check identified no issues (0 silenced).
  245. June 21, 2018 - 09:49:24
  246. Django version 1.8.17, using settings 'archer.settings'
  247. Starting development server at http://192.168.1.34:9123/
  248. Quit the server with CONTROL-C.
  249. 注:WEB访问时报错
  250. --创建archer系统登录用户
  251. pip3 install simplejson
  252.  通过浏览器访问http://10.10.3.70:8080/admin/sql/users/
  253. 可以看到django登录界面:
  254. 使用上面创建的用户名密码(admin/和密码)登录:
  255. 点击右侧Add用户配置,用户名密码自定义,至少创建一个工程师和一个审核人(用admin用户可以登录)后续新的工程师和审核人用户请用LDAP导入sql_users表或django admin增加
  256. --配置主库地址
  257. 通过浏览器访问http://10.10.3.70:8080/admin/sql/master_config
  258. 点击右侧Add master_config。这一步是为了告诉archer你要用inception去哪些mysql主库里执行SQL,所用到的用户名密码、端口等。
  259. --正式访问主页
  260. http://10.10.3.70:8080/login/
  261. 发起脚本如:
  262. use mysql;
  263. create table t (id int(10));
  264. insert into t values(1);
  265. insert into t values(2);
  266. commit;
  267. select * from t;
  268. 如报错:/opt/python3/lib/python3.4/site-packages/pymysql/connections.py in _request_authentication, line 1113
  269. 解决办法:
  270. vi /opt/python3/lib/python3.4/site-packages/pymysql/connections.py
  271. ... ...
  272. def _request_authentication(self):
  273. # https://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::HandshakeResponse
  274. self.server_version = '5.6.24-72.2-log' #添加此行
  275. if int(self.server_version.split('.', 1)[0]) >= 5:
  276. ... ...
  277. 如果遇到rollback的问题,就是给权限的问题,inception配置的连接的数据库的用户的权限,在
  278. grant select,create,insert on *.* to incep_rw@'%' identified by 'P@ssw0rd';
  279. 也就是我配置的admin的用户。
  280. inception源码地址:https://github.com/mysql-inception/inception
  281. archer 源码地址:https://github.com/jly8866/archer

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/1004818
推荐阅读
相关标签
  

闽ICP备14008679号