技术分享 | 将GreatSQL添加到系统systemd服务

欢迎来到 GreatSQL社区分享的MySQL技术文章,如有疑问或想学习的内容,可以在下方评论区留言,看到后会进行解答

  • GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源。

1、关于systemd

systemd 是Linux系统启动和服务器守护进程管理器,负责在系统启动或运行时,激活系统资源,服务器进程和其它进程,systemd被设计用来改进原来sysvinit中的多个缺点。

CentOS 7的systemd服务程序脚本存放在 /usr/lib/systemd/目录下,并区分 system 和 user,每一个服务程序脚本以 .service 结尾,例如 /usr/lib/systemd/system/sshd.service。

2、编辑systemd服务程序脚本

设定 GreatSQL 二进制文件放在 /usr/local/GreatSQL-8.0.23-14/ 目录下,即设定 basedir 为此目录,先进入到这个工作目录中。

[root@greatsql~]# cd /usr/local/GreatSQL-8.0.23-14/

复制 support-files/greatsql.server 程序脚本到 /usr/lib/systemd/system/ 目录下:

[root@greatsql~]# cp -f ./support-files/greatsql.server /usr/lib/systemd/system/

该脚本内容如下,基本上不需要再修改什么内容:

[root@greatsql~]# cat /usr/lib/systemd/system/greatsql.service

[Unit]
Description=GreatSQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
Type=notify
TimeoutSec=0
PermissionsStartOnly=true
ExecStartPre=/usr/local/GreatSQL-8.0.23-14/bin/mysqld_pre_systemd
ExecStart=/usr/local/GreatSQL-8.0.23-14/bin/mysqld $MYSQLD_OPTS
EnvironmentFile=-/etc/sysconfig/mysql
LimitNOFILE = 10000
Restart=on-failure
RestartPreventExitStatus=1
Environment=MYSQLD_PARENT_PID=1
PrivateTmp=false

3、准备my.cnf及其他配置文件

复制 support-files/my.cnf 到 /etc/ 目录下,替换原来的配置文件(原来的 /etc/my.cnf 建议先备份),并确认 datadir、port、server_id 等参数是否要修改:

[root@greatsql~]# cp -f ./support-files/my.cnf /etc/my.cnf

[root@greatsql~]# cat /etc/my.cnf
#my.cnf
[mysqld]
user = mysql
port = 3306
server_id = 3306
basedir=/usr/local/GreatSQL-8.0.23-14
datadir = /data/GreatSQL
socket = /data/GreatSQL/mysql.sock
pid-file = mysql.pid
character-set-server = UTF8MB4
skip_name_resolve = 1
#若你的MySQL数据库主要运行在*,请务必根据实际情况调整本参数
default_time_zone = "+8:00"

#performance setttings
lock_wait_timeout = 3600
open_files_limit    = 65535
back_log = 1024
max_connections = 512
max_connect_errors = 1000000
table_open_cache = 1024
table_definition_cache = 1024
thread_stack = 512K
sort_buffer_size = 4M
join_buffer_size = 4M
read_buffer_size = 8M
read_rnd_buffer_size = 4M
bulk_insert_buffer_size = 64M
thread_cache_size = 768
interactive_timeout = 600
wait_timeout = 600
tmp_table_size = 32M
max_heap_table_size = 32M

#log settings
log_timestamps = SYSTEM
log_error = /data/GreatSQL/error.log
log_error_verbosity = 3
slow_query_log = 1
log_slow_extra = 1
slow_query_log_file = /data/GreatSQL/slow.log
long_query_time = 0.1
log_queries_not_using_indexes = 1
log_throttle_queries_not_using_indexes = 60
min_examined_row_limit = 100
log_slow_admin_statements = 1
log_slow_slave_statements = 1
log_bin = /data/GreatSQL/binlog
binlog_format = ROW
sync_binlog = 1
binlog_cache_size = 4M
max_binlog_cache_size = 2G
max_binlog_size = 1G
binlog_rows_query_log_events = 1
binlog_expire_logs_seconds = 604800
#MySQL 8.0.22前,想启用MGR的话,需要设置binlog_checksum=NONE才行
binlog_checksum = CRC32
gtid_mode = ON
enforce_gtid_consistency = TRUE

#myisam settings
key_buffer_size = 32M
myisam_sort_buffer_size = 128M

#replication settings
master_info_repository = TABLE
relay_log_info_repository = TABLE
relay_log_recovery = 1
slave_parallel_type = LOGICAL_CLOCK
#可以设置为逻辑CPU数量的2倍
slave_parallel_workers = 64
binlog_transaction_dependency_tracking = WRITESET
slave_preserve_commit_order = 1
slave_checkpoint_period = 2

#mgr settings
loose-plugin_load_add = 'mysql_clone.so'
loose-plugin_load_add = 'group_replication.so'
loose-group_replication_group_name = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaa1"
#MGR本地节点IP:PORT,请自行替换
loose-group_replication_local_address = "172.16.16.10:33061"
#MGR集群所有节点IP:PORT,请自行替换
loose-group_replication_group_seeds = "172.16.16.10:33061,172.16.16.11:33061,172.16.16.12:33061"
loose-group_replication_start_on_boot = OFF
loose-group_replication_bootstrap_group = OFF
loose-group_replication_exit_state_action = READ_ONLY
loose-group_replication_flow_control_mode = "DISABLED"
loose-group_replication_single_primary_mode = ON

#innodb settings
transaction_isolation = REPEATABLE-READ
innodb_buffer_pool_size = 2G
innodb_buffer_pool_instances = 8
innodb_data_file_path = ibdata1:12M:autoextend
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 32M
innodb_log_file_size = 1G
innodb_log_files_in_group = 3
innodb_max_undo_log_size = 4G
# 根据您的服务器IOPS能力适当调整
# 一般配普通SSD盘的话,可以调整到 10000 - 20000
# 配置高端PCIe SSD卡的话,则可以调整的更高,比如 50000 - 80000
innodb_io_capacity = 4000
innodb_io_capacity_max = 8000
innodb_open_files = 65535
innodb_flush_method = O_DIRECT
innodb_lru_scan_depth = 4000
innodb_lock_wait_timeout = 10
innodb_rollback_on_timeout = 1
innodb_print_all_deadlocks = 1
innodb_online_alter_log_max_size = 4G
innodb_print_ddl_logs = 1
innodb_status_file = 1
#注意: 开启 innodb_status_output & innodb_status_output_locks 后, 可能会导致log_error文件增长较快
innodb_status_output = 0
innodb_status_output_locks = 1
innodb_sort_buffer_size = 67108864

#innodb monitor settings
innodb_monitor_enable = "module_innodb"
innodb_monitor_enable = "module_server"
innodb_monitor_enable = "module_dml"
innodb_monitor_enable = "module_ddl"
innodb_monitor_enable = "module_trx"
innodb_monitor_enable = "module_os"
innodb_monitor_enable = "module_purge"
innodb_monitor_enable = "module_log"
innodb_monitor_enable = "module_lock"
innodb_monitor_enable = "module_buffer"
innodb_monitor_enable = "module_index"
innodb_monitor_enable = "module_ibuf_system"
innodb_monitor_enable = "module_buffer_page"
innodb_monitor_enable = "module_adaptive_hash"

#pfs settings
performance_schema = 1
#performance_schema_instrument = '%memory%=on'
performance_schema_instrument = '%lock%=on'

再复制 support-files/sysconfig/mysql 文件到 /etc/sysconfig 目录下。

[root@greatsql~]# cp -f ./support-files/sysconfig/mysql /etc/sysconfig/mysql

[root@greatsql~]# cat /etc/sysconfig/mysql
LD_PRELOAD=/usr/lib64/libjemalloc.so.1

这是为了能让GreatSQL启动时加载jemalloc,获得更好的内存管理性能。

4、启动GreatSQL服务

上述准备工作完成后,还要将 GreatSQL 服务正式加载到 systemd 列表中:

[root@greatsql~]# systemctl daemon-reload

这就会自动将GreatSQL这个新服务加载进来了。

然后开始启动服务:

[root@greatsql~]# systemctl start greatsql

#如果加上 .service 后缀也可以,例如
[root@greatsql~]# systemctl start greatsql.service

如果启动失败,可能会输出类似的报错提示:

[root@greatsql~]# systemctl start greatsql
Job for greatsql.service failed because the control process exited with error code. See "systemctl status greatsql.service" and "journalctl -xe" for details.

按照错误提示,执行 systemctl status greatsql.service 或 journalctl -xe 查看失败的原因,亦可查看系统日志 /var/log/messages 或 GreatSQL日志 /data/GreatSQL/error.log 确认失败原因。

第一次会比较慢,因为还要初始化InnoDB系统表空间、redo、undo日志等。初始化时,还会为root生成临时密码,例如这样:

...
[MY-010454] [Server] A temporary password is generated for root@localhost: dofI,0HE#E>C
...

首次登入需要用这个密码,并立即修改成新密码,新密码还必须符合安全规范才行,不能是弱密码:

# 先登入
[root@greatsql~]# mysql -uroot -p -S/data/GreatSQL/mysql.sock
Enter password: ************

# 而后修改密码,否则不能做其他操作
[root@GreatSQL][(none)]> ALTER USER CURRENT_USER() IDENTIFIED BY 'GreatSQL3##)^';

这是因为用默认加载了 validate_password 组件(component),对密码有合规性要求。

[root@GreatSQL][(none)]> SELECT * FROM mysql.component;
+--------------+--------------------+------------------------------------+
| component_id | component_group_id | component_urn                      |
+--------------+--------------------+------------------------------------+
|            1 |                  1 | file://component_validate_password |
+--------------+--------------------+------------------------------------+

如果嫌麻烦可以卸载掉(但非常不建议这么做):

[root@GreatSQL][(none)]> UNINSTALL COMPONENT 'file://component_validate_password';

最后,介绍systemctl主要几个的命令:

  • 重载系统服务:systemctl daemon-reload

  • 启用开机启动:systemctl enable greatsql

  • 禁用开机启动:systemctl disable greatsql

  • 启动服务:systemctl start greatsql

  • 停止服务:systemctl stop greatsql

  • 重启服务:systemctl restart greatsql

  • 查看服务状态:systemctl status greatsql

全文完。

Enjoy GreatSQL

上一篇:GreatSQL重磅特性,InnoDB并行并行查询优化测试


下一篇:故障案例 | 一次慢SQL优化分析全过程