diff options
author | root <root@vps-18a7a2b7.vps.ovh.ca> | 2024-02-14 12:23:06 -0500 |
---|---|---|
committer | root <root@vps-18a7a2b7.vps.ovh.ca> | 2024-02-14 12:23:06 -0500 |
commit | fc787c8fb83821138915049e64c01eb4ad1060d7 (patch) | |
tree | e5cc569fd58a04c9bbbc8803d43f57abc60df708 | |
parent | 2482cc0fe4b4439628c7460e1ac1dcd6a4b282fc (diff) |
omfg replication actually works
-rwxr-xr-x | src/mariadb-push-replica.sh | 192 |
1 files changed, 131 insertions, 61 deletions
diff --git a/src/mariadb-push-replica.sh b/src/mariadb-push-replica.sh index 56a3b42..7d92859 100755 --- a/src/mariadb-push-replica.sh +++ b/src/mariadb-push-replica.sh | |||
@@ -198,16 +198,18 @@ mostly_silent_unless_error() | |||
198 | mariadb_install_replication_credentials() | 198 | mariadb_install_replication_credentials() |
199 | { | 199 | { |
200 | set -e | 200 | set -e |
201 | tee /dev/stderr <<END | mariadb --skip-reconnect -t | 201 | mariadb -v --skip-reconnect -t <<. |
202 | stop slave | 202 | stop slave |
203 | ; | 203 | ; |
204 | change master | 204 | change master |
205 | to | 205 | to |
206 | master_host='$1' | 206 | master_host = '$1' |
207 | , master_user='$2' | 207 | , master_user = '$2' |
208 | , master_password='$3' | 208 | , master_password = '$3' |
209 | , master_ssl = 1 | ||
210 | , master_ssl_verify_server_cert = 1 | ||
209 | ; | 211 | ; |
210 | END | 212 | . |
211 | } | 213 | } |
212 | 214 | ||
213 | enable_replication_via_mariabackup_xtra_info() | 215 | enable_replication_via_mariabackup_xtra_info() |
@@ -218,40 +220,63 @@ enable_replication_via_mariabackup_xtra_info() | |||
218 | mariabackup_target_dir=/var/mariadb/backup | 220 | mariabackup_target_dir=/var/mariadb/backup |
219 | binlog_info_file=$mariabackup_target_dir/xtrabackup_binlog_info | 221 | binlog_info_file=$mariabackup_target_dir/xtrabackup_binlog_info |
220 | set -e | 222 | set -e |
221 | read master_log_file master_log_pos gtid_slave_pos < $binlog_info_file | 223 | read \ |
222 | tee /dev/stderr <<END | mariadb --skip-reconnect -t | 224 | master_log_file \ |
223 | STOP SLAVE; | 225 | master_log_pos \ |
224 | SET GLOBAL gtid_slave_pos = "$gtid_slave_pos"; | 226 | gtid_slave_pos \ |
225 | CHANGE MASTER TO | 227 | < $binlog_info_file |
226 | MASTER_HOST='$primary_host', | 228 | mariadb -v --skip-reconnect -t <<. |
227 | MASTER_USER='$replication_user', | 229 | stop slave; |
228 | MASTER_PASSWORD='$replication_password', | 230 | set global |
229 | MASTER_USE_GTID=slave_pos; | 231 | gtid_slave_pos = '$gtid_slave_pos' |
230 | START SLAVE; | 232 | ; |
231 | SHOW SLAVE STATUS\G | 233 | change master |
232 | END | 234 | to |
235 | master_host = '$primary_host', | ||
236 | master_user = '$replication_user', | ||
237 | master_password = '$replication_password', | ||
238 | master_use_gtid = slave_pos | ||
239 | ; | ||
240 | start slave; | ||
241 | show slave status\g | ||
242 | . | ||
233 | set -x | 243 | set -x |
234 | rm -r /var/mariadb/backup | 244 | rm -r /var/mariadb/backup |
235 | } | 245 | } |
236 | 246 | ||
237 | create_replication_user() | 247 | create_replication_user() |
238 | { | 248 | { |
239 | mariadb --skip-reconnect -t <<END | 249 | mariadb --skip-reconnect -t <<. |
240 | CREATE OR REPLACE USER '$2'@'$1' IDENTIFIED BY '$3'; | 250 | |
241 | GRANT REPLICATION SLAVE ON *.* TO '$2'@'$1'; | 251 | create or replace |
242 | END | 252 | user |
253 | '$2'@'$1' | ||
254 | identified by | ||
255 | '$3' | ||
256 | ; | ||
257 | |||
258 | grant replication slave | ||
259 | on | ||
260 | *.* | ||
261 | to | ||
262 | '$2'@'$1' | ||
263 | ; | ||
264 | . | ||
243 | } | 265 | } |
244 | 266 | ||
245 | showvars() | 267 | showvars() |
246 | { | 268 | { |
247 | for h in "$@" | 269 | mariadb --skip-reconnect -t <<. |
248 | do | 270 | select |
249 | run_$h mariadb --skip-reconnect -t <<END | 271 | variable_name |
250 | select VARIABLE_NAME,SESSION_VALUE,GLOBAL_VALUE | 272 | , session_value |
251 | from INFORMATION_SCHEMA.SYSTEM_VARIABLES | 273 | , global_value |
252 | where VARIABLE_NAME LIKE '%SLAVE%' \G | 274 | from |
253 | END | 275 | information_schema.system_variables |
254 | done | 276 | where |
277 | variable_name like '%slave%state%' | ||
278 | \G | ||
279 | . | ||
255 | } | 280 | } |
256 | 281 | ||
257 | check_input() | 282 | check_input() |
@@ -293,15 +318,41 @@ set_server_id() | |||
293 | set -e | 318 | set -e |
294 | chosen_id=$(truncated_machineid_decimal_string_int32) | 319 | chosen_id=$(truncated_machineid_decimal_string_int32) |
295 | [ "$chosen_id" -gt 1 ] | 320 | [ "$chosen_id" -gt 1 ] |
296 | mariadb --skip-reconnect -t <<END | 321 | mariadb -v --skip-reconnect -t <<END |
297 | set global server_id = $chosen_id; | 322 | set global server_id = $chosen_id; |
298 | END | 323 | END |
299 | } | 324 | } |
300 | 325 | ||
326 | mariadb_enable_semi_sync() | ||
327 | { | ||
328 | set -e | ||
329 | chosen_id=$(truncated_machineid_decimal_string_int32) | ||
330 | [ "$chosen_id" -gt 1 ] | ||
331 | mariadb -v --skip-reconnect -t <<END | ||
332 | stop slave io_thread; | ||
333 | set global rpl_semi_sync_master_enabled = on; | ||
334 | set global rpl_semi_sync_slave_enabled = on; | ||
335 | |||
336 | use information_schema; | ||
337 | select | ||
338 | @@hostname | ||
339 | , @@server_id | ||
340 | ; | ||
341 | select | ||
342 | variable_name | ||
343 | , variable_value | ||
344 | from | ||
345 | global_variables | ||
346 | where | ||
347 | variable_name like 'rpl_%' | ||
348 | ; | ||
349 | END | ||
350 | } | ||
351 | |||
301 | mariadb_list_databases() | 352 | mariadb_list_databases() |
302 | { | 353 | { |
303 | list_databases >&2 | 354 | list_databases >&2 |
304 | mariadb -Bsss "$@" <<. | 355 | mariadb --skip-reconnect -Bsss "$@" <<. |
305 | select | 356 | select |
306 | schema_name | 357 | schema_name |
307 | from | 358 | from |
@@ -409,43 +460,30 @@ mariadb_scan_databases() | |||
409 | 460 | ||
410 | choose_mariadbdump_target_databases() | 461 | choose_mariadbdump_target_databases() |
411 | { | 462 | { |
412 | declare -n target_databases="$1" | 463 | declare -n _target_db_array="$1" |
413 | shift | 464 | shift |
414 | if [ $# = 0 ] | 465 | if [ $# = 0 ] |
415 | then | 466 | then |
416 | target_databases=() | 467 | if [ ${#primary_dbs_not_on_replica[@]} -gt 0 ] |
417 | [ ${#primary_dbs_not_on_replica[@]} -gt 0 ] || return 0 | 468 | then |
418 | printf "Missing on ${replica_host}: %s\n" \ | 469 | printf "Missing on ${replica_host}: %s\n" \ |
419 | "${primary_dbs_not_on_replica[@]}" >&2 | 470 | "${primary_dbs_not_on_replica[@]}" >&2 |
420 | [ "$SEND_ALL_MARIADB_DATABASES" ] || return 0 | 471 | fi |
421 | target_databases=("${primary_dbs_not_on_replica[@]}") | 472 | |
473 | if [ "$SEND_ALL_MARIADB_DATABASES" ] | ||
474 | then | ||
475 | _target_db_array=("${primary_dbs_not_on_replica[@]}") | ||
476 | else | ||
477 | _target_db_array=() | ||
478 | fi | ||
422 | else | 479 | else |
423 | save_array target_databases from lines \ | 480 | save_array _target_db_array from lines \ |
424 | <(intersection_lines \ | 481 | <(intersection_lines \ |
425 | <(printarray primary_dbs_not_on_replica) \ | 482 | <(printarray primary_dbs_not_on_replica) \ |
426 | <(printlines "$@" | sort -u)) | 483 | <(printlines "$@" | sort -u)) |
427 | fi | 484 | fi |
428 | } | 485 | } |
429 | 486 | ||
430 | mariadbdump_transfer_missing_databases() | ||
431 | { | ||
432 | mariadbdump_transfer_databases \ | ||
433 | "$replica_host" \ | ||
434 | "${to_replicate[@]}" | ||
435 | } | ||
436 | |||
437 | mariadbdump_transfer_databases() | ||
438 | { | ||
439 | [ $# -ge 2 ] || return 0 | ||
440 | run_primary \ | ||
441 | send_mariadb_dump "$1" \ | ||
442 | --master-data \ | ||
443 | --apply-slave-statements \ | ||
444 | --gtid \ | ||
445 | --single-transaction \ | ||
446 | --databases "${@:2}" | ||
447 | } | ||
448 | |||
449 | intersection_lines() | 487 | intersection_lines() |
450 | { | 488 | { |
451 | comm -12 -- "$1" "$2" | 489 | comm -12 -- "$1" "$2" |
@@ -465,6 +503,7 @@ main() | |||
465 | check_input | 503 | check_input |
466 | 504 | ||
467 | run_both set_server_id | 505 | run_both set_server_id |
506 | run_both mariadb_enable_semi_sync | ||
468 | 507 | ||
469 | # run_primary check_db | 508 | # run_primary check_db |
470 | # run_replica check_db | 509 | # run_replica check_db |
@@ -482,7 +521,8 @@ main() | |||
482 | "$replication_user" \ | 521 | "$replication_user" \ |
483 | "$replication_password" | 522 | "$replication_password" |
484 | run_primary \ | 523 | run_primary \ |
485 | send_mariadb_dump "$replica_host" \ | 524 | send_mariadb_dump \ |
525 | "$replica_host" \ | ||
486 | --master-data \ | 526 | --master-data \ |
487 | --apply-slave-statements \ | 527 | --apply-slave-statements \ |
488 | --gtid \ | 528 | --gtid \ |
@@ -490,6 +530,34 @@ main() | |||
490 | --databases "${to_replicate[@]}" | 530 | --databases "${to_replicate[@]}" |
491 | run_replica list_databases | 531 | run_replica list_databases |
492 | fi | 532 | fi |
533 | #run_replica mariadb <<< 'show slave status\G' | ||
534 | #run_primary mariadb <<< 'show master status\G' | ||
535 | #run_replica showvars | ||
536 | #run_replica mariadb <<< 'show slave status\G' | ||
537 | gtid=$(run_primary mariadb --skip-reconnect -sssB <<< \ | ||
538 | 'select @@gtid_binlog_pos;') | ||
539 | [ "$gtid" ] && run_replica wait_on_gtid "$gtid" | ||
540 | } | ||
541 | |||
542 | wait_on_gtid() | ||
543 | { | ||
544 | local gtid="$1" | ||
545 | mariadb -t <<. | ||
546 | select | ||
547 | @@hostname | ||
548 | , @@gtid_slave_pos as 'slave gtid' | ||
549 | , '$gtid' as 'master gtid' | ||
550 | ; | ||
551 | stop slave ; | ||
552 | stop slave io_thread ; | ||
553 | start slave ; | ||
554 | start slave io_thread ; | ||
555 | . | ||
556 | mariadb -t -v <<. | ||
557 | select | ||
558 | master_gtid_wait('$gtid') | ||
559 | ; | ||
560 | . | ||
493 | } | 561 | } |
494 | 562 | ||
495 | cleanup_after_test() | 563 | cleanup_after_test() |
@@ -504,12 +572,14 @@ select | |||
504 | @@hostname | 572 | @@hostname |
505 | , @@server_id | 573 | , @@server_id |
506 | , count(schema_name) as 'databases' | 574 | , count(schema_name) as 'databases' |
575 | , user() as 'login' | ||
507 | , @@gtid_slave_pos | 576 | , @@gtid_slave_pos |
577 | , @@gtid_binlog_pos | ||
508 | from | 578 | from |
509 | information_schema.schemata | 579 | information_schema.schemata |
510 | ; | 580 | \G |
511 | select | 581 | select |
512 | schema_name as 'database' | 582 | schema_name as 'database name' |
513 | from | 583 | from |
514 | information_schema.schemata | 584 | information_schema.schemata |
515 | ; | 585 | ; |