12 Star 67 Fork 21

小螺旋丸 / mycat-encrypt-server

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
GPL-2.0

介绍

本中间件在 mycat 源码基础上进行了优化和改造,支持数据库列级别的透明加解密,兼容 mysql8。

支持 mysql 原生协议,支持在配置文件中以 jdbc 驱动的方式连接其他类型数据库,但并不推荐,mycat 是 mysql 最佳伴侣,其他数据库可能存在诸多不稳定因素。

本中间件的用法跟 mycat 基本一致,在原来的基础上添加了配置文件 encrypt.xml 用于加密的相关配置,server.xml 里增加了几个加密的属性。

这里着重介绍一下具体用法和加密部分的配置,其他部分的配置可参考 mycat,另外,分库分表不在万不得已不要轻易使用,mycat 的分库分表是一个伪分布式数据库,日后如果要扩充节点调整规则会十分麻烦,其实大部分公司也只用了它的读写分离功能。

关注公众号 螺旋编程极客 ,可进群一起探讨,共同学习进步,同时可领取海量学习资源。 螺旋编程极客

安装

下载 release 压缩包,直接解压即可。

v1.6.7.8-release下载

运行

linux

./mycat start 启动

./mycat stop 停止

./mycat console 前台运行

./mycat restart 重启服务

./mycat pause 暂停

./mycat status 查看启动状态

win

直接运行startup_nowrap.bat,如果出现闪退,在cmd 命令行运行,查看出错原因。

内存配置

打开 conf/wrapper.conf 文件,可以调整 jvm 的相关参数。

连接测试

测试 mycat 与测试 mysql 完全一致,mysql 怎么连接,mycat 就怎么连接。

mycat 的默认服务端口为 8066: mysql -u 用户名 -P 8066 -p 密码,程序或 navicat 等工具连接使用此端口

mycat 的默认管理端口为 9066: mysql -u 用户名 -P 9066 -p 密码,配合 mycat-encrypt-web 用于监控管理,常用指令如 show @@sql,show @@sql.slow,show @@sql.high 等

mycat 完全支持 mysql5.7,目前市面上的主流 mysql 版本仍然是 5.7,包括分布式数据库 TiDB 也是对 mysql5.7 进行适配。

如果要使用 mysql8.0,需要把用户的密码认证插件设置为 mysql_native_password,

对已经创建过的用户执行命令:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

修改 my.cnf 配置文件,之后创建的用户将默认使用 mysql_native_password:

[mysqld]
default_authentication_plugin=mysql_native_password

配置说明

--bin 启动目录

--conf 配置目录存放配置文件:

  --server.xml:是Mycat服务器参数调整和用户授权的配置文件。

  --schema.xml:是逻辑库定义和表以及分片定义的配置文件。
  
  --encrypt.xml 是加密指定库,表,字段的配置文件(新添加的模块)。

  --rule.xml:  是分片规则的配置文件,分片规则的具体一些参数信息单独存放为文件,也在这个目录下,配置文件修改需要重启MyCAT。

  --log4j.xml: 日志存放在logs/log中,每天一个文件,日志的配置是在conf/log4j.xml中,根据自己的需要可以调整输出级别为debug                           debug级别下,会输出更多的信息,方便排查问题。

  --autopartition-long.txt,partition-hash-int.txt,sequence_conf.properties, sequence_db_conf.properties 分片相关的id分片规则配置文件

  --lib	   MyCAT自身的jar包或依赖的jar包的存放目录。

  --logs   MyCAT日志的存放目录。日志存放在logs/log中,每天一个文件

逻辑库配置

编辑schema.xml

schema 是实际逻辑库的配置,可配置多个

dataNode 是逻辑库对应的分片

dataHost 是实际的物理库配置地址,可以配置多主主从等其他配置,多个 dataHost 代表分片对应的物理库地址,下面的 writeHost、readHost 代表该分片是否配置多写,主从,读写分离等高级特性。

balance 属性

负载均衡类型,目前的取值有 3 种:

  1. balance="0", 不开启读写分离机制,所有读操作都发送到当前可用的 writeHost 上。
  2. balance="1",全部的 readHost 与 stand by writeHost 参与 select 语句的负载均衡,简单的说,当双 主双从模式(M1->S1,M2->S2,并且 M1 与 M2 互为主备),正常情况下,M2,S1,S2 都参与 select 语句的负载 均衡。
  3. balance="2",所有读操作都随机的在 writeHost、readhost 上分发。
  4. balance="3",所有读请求随机的分发到 wiriterHost 对应的 readhost 执行,writerHost 不负担读压力,

writeType 属性

负载均衡类型,目前的取值有 3 种:

  1. writeType="0", 所有写操作发送到配置的第一个 writeHost,第一个挂了切到还生存的第二个 writeHost, 重新启动后以切换后的为准,切换记录在配置文件中:dnindex.properties 。
  2. writeType="1",所有写操作都随机的发送到配置的 writeHost,1.5 以后废弃不推荐。

switchType 属性

-1 表示不自动切换

1 默认值,自动切换

2 基于 MySQL 主从同步的状态决定是否切换

心跳语句为 show slave status

3 基于 MySQL galary cluster 的切换机制(适合集群)

心跳语句为 show status like ‘wsrep%’

    <schema name="test" checkSQLschema="true" sqlMaxLimit="100" dataNode="test">
    </schema>

    <dataNode name="test" dataHost="test" database="test" />

    <dataHost name="test" maxCon="1000" minCon="10" balance="0"
              writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
        <heartbeat>select user()</heartbeat>
        <!-- can have multi write hosts -->
        <writeHost host="hostM1" url="localhost:3307" user="root"
                   password="root">
        </writeHost>
    </dataHost>

配置server.xml

添加mycat逻辑库:test(多个用逗号隔开)

use 可配置多个,实现多租户。

 <user name="root">
        <property name="password">123456</property>
        <property name="schemas">test</property>
 </user>

新增加密相关属性:

<!--是否启用加密-->
<property name="encrypt">1</property>
<!--是否对返回结果解密-->
<property name="decrypt">1</property>
<!--是否使用内置的加密算法 1-是 0-否 内置的加密算法支持加密后的 like 查询 -->
<!--如果要自定义加密算法,下载源码 mycat-encrypt-customize 自行实现 encode 和 decode 方法,打包替换 lib 下的			encrypt-core-2.0.jar 即可-->
<property name="useInternalEncryptAlgorithm">1</property>
<!--内置加密算法的密钥(自定义16位字符串) 如果自定义加密算法,则不需要配置-->
<property name="secretKey">12345678ABCDEFGH</property>

加密配置

编辑 encrypt.xml

	<!--数据库编码需要为utf-8或ut8mb64类型 以下所有属性皆为必填项目-->
    <!--加密的此处的 schema 对应 schema.xml 中的schema,schema 保持唯一,不能重复-->
    <server schema="test">
        <!--auto 设置为1会自动对配置的字段进行加密,dbType 目前只支持 mysql-->
        <!--datasource 的 name 属性跟 schema 的 database 属性保持一致-->
        <datasource name="test" ip="localhost" port="3307" username="root" password="root" auto="1" dbType="mysql">
            <!--需要加密的字段名称-->
            <column name="card_code">
                <!--需要加密的表名和主键 程序会根据主键更新加密字段的值-->
                <table name="real_people_copy1" pk="people_id"/>
            </column>
            <column name="name">
                <table name="real_people_copy1" pk="people_id"/>
            </column>
        </datasource>
    </server>

自定义加密算法

如果要自定义加密算法,下载源码包 mycat-encrypt-customize 自行实现 encode 和 decode 方法,打包替换 lib 下的 encrypt-core-2.0.jar 即可

手动解密

如果要恢复已经加密的数据,可以下载解密工具包,使用方法跟 mycat 一致,配置好 decrypt.xml 即可自动解密,解密完毕后程序会自动退出。

下载解密工具

关注公众号 螺旋编程极客 ,可进群一起探讨,共同学习进步,同时可领取海量学习资源。 螺旋编程极客

GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc., <http://fsf.org/> 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. {description} Copyright (C) {year} {fullname} This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. {signature of Ty Coon}, 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License.

简介

基于 mycat 改造的数据库代理透明加密中间件,支持所有 mycat 已有的功能,在原来的代码基础上做了优化,修复了 mycat 存在的一些bug,支持 mysql8;添加了透明加解密功能模块,可对数据库进行列级别的加密存储,对应用无感知,无需修改业务代码。 展开 收起
GPL-2.0
取消

发行版 (3)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/zrxjava/mycat-encrypt-server.git
git@gitee.com:zrxjava/mycat-encrypt-server.git
zrxjava
mycat-encrypt-server
mycat-encrypt-server
master

搜索帮助