创建MySQL实例:
添加实例:/opt/sequoiasql/mysql/bin/sdb_sql_ctl addinst myinst -D database/3306/
查看实例:/opt/sequoiasql/mysql/bin/sdb_sql_ctl listinst
查看实例状态:/opt/sequoiasql/mysql/bin/sdb_sql_ctl status
创建数据库:
登录Mysql:/opt/sequoiasql/mysql/bin/mysql -h 127.0.0.1 -P 3306 -u root
MySQL实例的高可用配置:
创建数据库同步的用户:
登录mysql:/opt/sequoiasql/mysql/bin/mysql -h 127.0.0.1 -P 3306 -u root
创建用于同步元数据的MySQL用户:CREATE USER 'sdbadmin'@'%' IDENTIFIED BY 'sdbadmin';
授予所有权限,用户名和密码在所有实例上保持一致:GRANT ALL ON *.* TO 'sdbadmin'@'%' WITH GRANT OPTION;
刷新权限:FLUSH PRIVILEGES;
审计插件部署:
审计插件准备:
检查MySQL安装目录下tools/lib目录的审计插件:ls /opt/sequoiasql/mysql/tools/lib/server_audit.so
检查 MySQL 安装目录下 lib/plugin 目录的审计插件:ls /opt/sequoiasql/mysql/lib/plugin/server_audit.so
将审计插件 server_audit.so 文件复制到 MySQL 安装目录中的 lib/plugin 目录下:cp /opt/sequoiasql/mysql/tools/lib/server_audit.so /opt/sequoiasql/mysql/lib/plugin/
赋予 MySQL 运行用户的可执行权限:chmod a+x /opt/sequoiasql/mysql/lib/plugin/server_audit.so
检查 MySQL 安装目录下 lib/plugin 目录的审计插件是否存在:ls -lat /opt/sequoiasql/mysql/lib/plugin/server_audit.so
审计插件配置:
修改 MySQL 实例的配置文件:
echo 'plugin-load=server_audit=server_audit.so' >> /opt/sequoiasql/mysql/database/3306/auto.cnf
echo 'server_audit_logging=ON' >> /opt/sequoiasql/mysql/database/3306/auto.cnf
echo 'server_audit_file_path=/opt/sequoiasql/mysql/database/auditlog/server_audit.log' >> /opt/sequoiasql/mysql/database/3306/auto.cnf
echo 'server_audit_file_rotate_now=OFF' >> /opt/sequoiasql/mysql/database/3306/auto.cnf
echo 'server_audit_file_rotate_size=10485760' >> /opt/sequoiasql/mysql/database/3306/auto.cnf
echo 'server_audit_file_rotations=999' >> /opt/sequoiasql/mysql/database/3306/auto.cnf
echo 'server_audit_output_type=file' >> /opt/sequoiasql/mysql/database/3306/auto.cnf
echo 'server_audit_query_log_limit=102400' >> /opt/sequoiasql/mysql/database/3306/auto.cnf
创建审计日志存放的文件夹:mkdir /opt/sequoiasql/mysql/database/auditlog/
元数据同步工具部署:
拷贝生成一份元数据同步工具的配置文件:cp /opt/sequoiasql/mysql/tools/metaSync/config.sample /opt/sequoiasql/mysql/tools/metaSync/config
进行元数据同步工具配置文件修改:sed -i 's/hosts = sdb1,sdb2,sdb3/hosts = sdbserver1,sdbserver2/g' /opt/sequoiasql/mysql/tools/metaSync/config
确认配置文件 hosts 参数是否为 sdbserver1,sdbserver2 :cat /opt/sequoiasql/mysql/tools/metaSync/config
拷贝一份元数据同步的日志配置文件同步工具使用 python 的 logging 模块输出日志,配置文件为 log.config。如果是全新安装,开始该文件是不存在的,需要从 log.config.sample 拷贝。配置项如下(日志目录会自动创建):cp /opt/sequoiasql/mysql/tools/metaSync/log.config.sample /opt/sequoiasql/mysql/tools/metaSync/log.config
启动元数据同步工具:
在完成所有配置后,在各实例所在主机的 sdbadmin 用户下,执行以下命令在后台启动同步工具:python /opt/sequoiasql/mysql/tools/metaSync/meta_sync.py &
可以通过配置定时任务提供基本的同步工具监控,定期检查程序是否在运行,若进程退出了,会被自动拉起。配置命令如下(在 SequoiaSQL-MySQL 安装用户下配置):crontab -e
去到最后一行按 i 然后添加以下内容,然后保存退出:
#每一分钟运行一次
*/1 * * * * /usr/bin/python /opt/sequoiasql/mysql/tools/metaSync/meta_sync.py >/dev/null 2>&1 &
设置本实例连接巨杉数据库的协调节点:sed -i 's/# sequoiadb_conn_addr=localhost:11810/sequoiadb_conn_addr=172.17.0.1:11810/g' database/3306/auto.cnf
事务提交和回滚:
MySQL:
事务提交:
开启事务操作:Begin;
执行SQL语句,包含写入与更新:
INSERT INTO employee (ename, age) VALUES ("Ben", 25);
UPDATE employee SET age = 22 WHERE ename = "Alice";
执行事务提交操作:commit;
事务回滚:
MySQL实例事务管理:
MySQL 实例的事务是基于 SequoiaDB 巨杉数据库存储引擎的,如果需要 MySQL 实例支持事务,存储引擎也必须开启事务,本小节将讲解如何查看并关闭 MySQL 的事务功能,并对关闭事务功能后的 MySQL 实例进行验证。
查看MySQL是否打开事务:SHOW VARIABLES LIKE '%sequoiadb_use_transaction%';
在shell中:关闭MySQL事务功能:
cat >> /opt/sequoiasql/mysql/database/3306/auto.cnf << EOF
sequoiadb_use_transaction = OFF
EOF
数据库实例导入导出
CSV导出:
导出导入参数配置:
在 auto.cnf 文件中加入导入导出路径的参数配置:
cat >> /opt/sequoiasql/mysql/bin/database/3306/auto.cnf << EOF
secure_file_priv = "/opt/sequoiasql/mysql/tmp"
EOF
创建存放数据路径:mkdir /opt/sequoiasql/mysql/tmp
登录MySQL:/opt/sequoiasql/mysql/bin/mysql -h 127.0.0.1 -P 3306 -u root
CSV格式数据导出:
执行 sql 语句进行导出, 其中字段以逗号(,)分隔,以双引号(")作为字符串标识符,以回车换行(\r\n)作为记录分割符:
SELECT * FROM employee
INTO OUTFILE '/opt/sequoiasql/mysql/tmp/employee_export.csv'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '"'
LINES TERMINATED BY '\r\n';
CSV导入:
在MySQL实例中创建导入测试的分区表emoloyee_import_test
USE company;
CREATE TABLE employee_import_test
(
empno INT AUTO_INCREMENT PRIMARY KEY,
ename VARCHAR(128),
age INT
)
ENGINE=sequoiadb COMMENT="雇员表, sequoiadb: { table_options: { ShardingKey: { 'empno': 1 }, ShardingType: 'hash', 'Compressed': true,'CompressionType': 'lzw', 'AutoSplit': true, 'EnsureShardingIndex': false } }";
CSV根式数据导入:
导入语句操作:
LOAD DATA INFILE '/opt/sequoiasql/mysql/tmp/employee_export.csv'
INTO TABLE employee_import_test
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '"'
LINES TERMINATED BY '\r\n';
mysqldump导出:
mysqldump 工具导出,路径为(/opt/sequoiasql/mysql/tmp/employee_import_test.sql):
/opt/sequoiasql/mysql/bin/mysqldump -h 127.0.0.1 -u root company employee_import_test > /opt/sequoiasql/mysql/tmp/employee_import_test.sql
Mysqldump导入:
清理数据表:
登录mysql:/opt/sequoiasql/mysql/bin/mysql -h 127.0.0.1 -P 3306 -u root
删除表 employee_import_test,为后面导入测试做准备:
USE company;
DROP TABLE employee_import_test;
mysqldump工具导入:
mysqldump 导出后生成的文件是sql语句的集合,可以直接执行:SOURCE /opt/sequoiasql/mysql/tmp/employee_import_test.sql;
数据库实例备份恢复:
进行数据备份:
mkdir /opt/sequoiasql/mysql/tmp
/opt/sequoiasql/mysql/bin/mysqldump -h 127.0.0.1 -u root company employee > /opt/sequoiasql/mysql/tmp/employee.sql
数据恢复:
进入SQL:/opt/sequoiasql/mysql/bin/mysql -h 127.0.0.1 -P 3306 -u root
删除数据库:DROP DATABASE company;
进行数据恢复,mysqldump 导出后生成的文件是 sql 语句的集合,可以直接执行:SOURCE /home/sdbadmin/mysqlbak/all.sql;
执行计划分析:
构造数据:
DROP PROCEDURE IF EXISTS gen_data;
USE company;
-- 创建存储过程
DELIMITER $$
CREATE PROCEDURE gen_data(IN num INT)
BEGIN
DECLARE i INT DEFAULT 0;
SET i = 0;
WHILE i < num DO
INSERT INTO employee VALUES (i, 'Name', 21);
SET i = i + 1;
END WHILE;
END $$
DELIMITER ;
-- 调用存储过程生成 1000 条数据
CALL gen_data(1000);
慢查询日志配置:
配置:
修改MySQL配置文件:
cat >> /opt/sequoiasql/mysql/database/3306/auto.cnf <# 开启慢查询日志
slow_query_log=on
# 慢查询日志存放路径
slow_query_log_file = /opt/sequoiasql/mysql/database/sdb-mysql-slow.log
# 超过多长时间的查询为慢查询
long_query_time = 1
EOF
分析慢查询SQL:mysqldumpslow /opt/sequoiasql/mysql/database/sdb-mysql-slow.log
创建索引:
观察索引对执行计划的影响,为数据表 employee 的 age 查询字段创建索引:CREATE INDEX age_Index ON employee(age);
再次执行计划分析:EXPLAIN SELECT * FROM employee WHERE age = 20;