配置用户
{
"password": "123456",
"transactionType": "proxy",
"username": "root"
}
{
"ip": "127.0.0.1",//建议为空,填写后会对客户端的ip进行限制
"password": "123456",
"transactionType": "proxy",
"username": "root"
}
{
"dialect":"mysql",
"ip":null,
"password":"123456",
"transactionType":"proxy",
"username":"root",
"isolation":3 //2022-3-25
}
{用户名}.user.json放在users文件夹下
password在密码检查在授权插件不同的情况下,Mycat2可能会获得空的密码,在mycat2不设置密码的情况下,设置空密码可以跳过检查密码
isolation
设置初始化的事务隔离级别
READ_UNCOMMITTED:1
READ_COMMITTED:2
REPEATED_READ:3,默认
SERIALIZABLE:4
transactionType
默认值:proxy
可选值:
proxy 本地事务,在涉及大于1个数据库的事务,commit阶段失败会导致不一致,但是兼容性最好
xa 事务,需要确认存储节点集群类型是否支持XA
事务类型在开启自动提交而且没有事务的时候可以使用以下语句实现切换
set transaction_policy = 'xa'
set transaction_policy = 'proxy'
SELECT @@transaction_policy
可以查询当前事务类型
无论哪种方式
应用代码编写都应该符合以下形式
连接中设置查询超时
Connection con = getConnection();
try{
start transcation;
commit/rollback;
}catche(Exception e){
rollback;//显式触发回滚
}finally{
con.close();
}
Connection con = getConnection();
try{
set autocommit = 0;
commit/rollback;
}catche(Exception e){
rollback;//显式触发回滚
}finally{
con.close();
//set autocommit = 1;//连接池隐式设置
}
encryptType
默认值:NONE
可选值:NONE,rsa
encodeKey
使用RSA的时候生效
Mycat会使用RSA和encodeKey解密出真正的密码,示例如下:
{
"dialect":"mysql",
"ip":null,
"password":"加密后的密码",
"transactionType":"xa",
"username":"root",
"encodeKey":"秘钥",
"encryptType":"rsa"
}
encodeKey,和加密password的生成
import java.io.UnsupportedEncodingException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;
public class RSAUtils {
public static void main(String[] args) throws Exception {
// 生成公钥和私钥
Map<Integer, String> keyMap = genKeyPair();
// 加密字符串
String password = "1234567";
System.out.println("随机生成的encodeKey:" + keyMap.get(1));
String messageEn = encrypt(password, keyMap.get(0));
System.out.println(password + "\n加密后的字符串(password)为:" + messageEn);
}
/**
* 随机生成密钥对
*
* @throws NoSuchAlgorithmException
*/
public static Map<Integer, String> genKeyPair()
throws NoSuchAlgorithmException, UnsupportedEncodingException {
// KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
// 初始化密钥对生成器,密钥大小为96-1024位
keyPairGen.initialize(1024, new SecureRandom());
// 生成一个密钥对,保存在keyPair中
KeyPair keyPair = keyPairGen.generateKeyPair();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); // 得到私钥
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); // 得到公钥
String publicKeyString = encodeBase64(publicKey.getEncoded());
// 得到私钥字符串
String privateKeyString = encodeBase64((privateKey.getEncoded()));
Map<Integer, String> keyMap = new HashMap<>(); // 用于封装随机产生的公钥与私钥
// 将公钥和私钥保存到Map
keyMap.put(0, publicKeyString); // 0表示公钥
keyMap.put(1, privateKeyString); // 1表示私钥
return keyMap;
}
/**
* RSA公钥加密
*
* @param str 加密字符串
* @param publicKey 公钥
* @return 密文
* @throws Exception 加密过程中的异常信息
*/
public static String encrypt(String str, String publicKey) throws Exception {
// base64编码的公钥
byte[] decoded = decodeBase64ToBytes(publicKey);
RSAPublicKey pubKey =
(RSAPublicKey)
KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
// RSA加密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
String outStr = encodeBase64(cipher.doFinal(str.getBytes("UTF-8")));
return outStr;
}
public static String encodeBase64(byte[] data) {
return encoder.encodeToString(data);
}
public static byte[] decodeBase64ToBytes(String data) {
return decoder.decode(data.getBytes());
}
public static Base64.Encoder encoder;
public static Base64.Decoder decoder;
static {
decoder = Base64.getDecoder();
encoder = Base64.getEncoder();
}
}