mysql主从搭建
程序员文章站
2022-07-04 08:52:36
mysql初始化/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --defaults-file=/etc/my3306.cnf --datadir=/home/mysql3306/mysql3306/ --us ......
mysql初始化
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --defaults-file=/etc/my3306.cnf --datadir=/home/mysql3306/mysql3306/ --user=mysql
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --defaults-file=/etc/my3306.cnf --datadir=/home/mysql3306/mysql3306/ --user=mysql
/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my3306.cnf --user=mysql &
/usr/local/mysql/bin/mysql -s /tmp/mysql3306.sock
主库,从库打开binlog(修改binlog需要重新启动数据库)
mysql> show variables like '%binlog%';
+-----------------------------------------+----------------------+
| variable_name | value |
+-----------------------------------------+----------------------+
| binlog_cache_size | 4194304 |
| binlog_checksum | crc32 |
| binlog_direct_non_transactional_updates | off |
| binlog_error_action | ignore_error |
| binlog_format | row |
| binlog_gtid_simple_recovery | off |
| binlog_max_flush_queue_time | 0 |
| binlog_order_commits | on |
| binlog_row_image | full |
| binlog_rows_query_log_events | off |
| binlog_stmt_cache_size | 32768 |
| binlogging_impossible_mode | ignore_error |
| innodb_api_enable_binlog | off |
| innodb_locks_unsafe_for_binlog | off |
| max_binlog_cache_size | 8388608 |
| max_binlog_size | 1073741824 |
| max_binlog_stmt_cache_size | 18446744073709547520 |
| simplified_binlog_gtid_recovery | off |
| sync_binlog | 1 |
+-----------------------------------------+----------------------+
19 rows in set (0.00 sec)
+-----------------------------------------+----------------------+
| variable_name | value |
+-----------------------------------------+----------------------+
| binlog_cache_size | 4194304 |
| binlog_checksum | crc32 |
| binlog_direct_non_transactional_updates | off |
| binlog_error_action | ignore_error |
| binlog_format | row |
| binlog_gtid_simple_recovery | off |
| binlog_max_flush_queue_time | 0 |
| binlog_order_commits | on |
| binlog_row_image | full |
| binlog_rows_query_log_events | off |
| binlog_stmt_cache_size | 32768 |
| binlogging_impossible_mode | ignore_error |
| innodb_api_enable_binlog | off |
| innodb_locks_unsafe_for_binlog | off |
| max_binlog_cache_size | 8388608 |
| max_binlog_size | 1073741824 |
| max_binlog_stmt_cache_size | 18446744073709547520 |
| simplified_binlog_gtid_recovery | off |
| sync_binlog | 1 |
+-----------------------------------------+----------------------+
19 rows in set (0.00 sec)
mysql>
主库:
mysql> show databases;清除多余的数据库
drop database ----
mysql> show databases;清除多余的数据库
drop database ----
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| file | position | binlog_do_db | binlog_ignore_db | executed_gtid_set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 1196 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
+------------------+----------+--------------+------------------+-------------------+
| file | position | binlog_do_db | binlog_ignore_db | executed_gtid_set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 1196 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> reset master;(有数据的话,就不要做这操作,紧急)
mysql> select user,host,password from mysql.user;
+------+-----------+----------+
| user | host | password |
+------+-----------+----------+
| root | localhost | |
| root | oggs | |
| root | 127.0.0.1 | |
| root | ::1 | |
| | localhost | |
| | oggs | |
+------+-----------+----------+
6 rows in set (0.00 sec)
| user | host | password |
+------+-----------+----------+
| root | localhost | |
| root | oggs | |
| root | 127.0.0.1 | |
| root | ::1 | |
| | localhost | |
| | oggs | |
+------+-----------+----------+
6 rows in set (0.00 sec)
mysql> delete from mysql.user where user='';
mysql> delete from mysql.user where host='::1';
query ok, 1 row affected (0.01 sec)
mysql> delete from mysql.user where host='::1';
query ok, 1 row affected (0.01 sec)
mysql> delete from mysql.user where host='oggs';
query ok, 1 row affected (0.01 sec)
query ok, 1 row affected (0.01 sec)
mysql> select user,host,password from mysql.user;
+------+-----------+----------+
| user | host | password |
+------+-----------+----------+
| root | localhost | |
| root | 127.0.0.1 | |
+------+-----------+----------+
2 rows in set (0.00 sec)
+------+-----------+----------+
| user | host | password |
+------+-----------+----------+
| root | localhost | |
| root | 127.0.0.1 | |
+------+-----------+----------+
2 rows in set (0.00 sec)
mysql> flush privileges;
query ok, 0 rows affected (0.00 sec)
query ok, 0 rows affected (0.00 sec)
在主服务器上建立帐户并授权slave:
mysql> grant replication client,replication slave on *.* to rep@'192.168.100.200' identified by 'rep’;(从端ip)
mysql> flush privileges;
查看主服务器状态
查看主库的状态,查看主库的binlog的?文件名字,以及?目前binlog的pos,从库需要读取binlog并且进?行行同步
查看主库的状态,查看主库的binlog的?文件名字,以及?目前binlog的pos,从库需要读取binlog并且进?行行同步
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| file | position | binlog_do_db | binlog_ignore_db | executed_gtid_set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
执?行行完此步骤后不要再操作主服务器mysql,防?止主服务器状态值变化
+------------------+----------+--------------+------------------+-------------------+
| file | position | binlog_do_db | binlog_ignore_db | executed_gtid_set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
执?行行完此步骤后不要再操作主服务器mysql,防?止主服务器状态值变化
从库:
mysql> show master status;
mysql> reset master;
mysql> reset master;
从库设置需要同步的主库
mysql> change master to master_host='192.168.100.100',master_user='rep',master_password='rep', master_log_file='mysql-bin.000007',master_log_pos=120,master_port=3306;
mysql> change master to master_host='192.168.100.100',master_user='rep',master_password='rep', master_log_file='mysql-bin.000007',master_log_pos=120,master_port=3306;
开启复制,关闭使用stop slave
start slave;
start slave;
主库关闭读锁
unlock table;
unlock table;
检查从服务器复制功能状态:
mysql> show slave status\g
*************************** 1
-----
slave_io_running: yes-----------------此状态必须yes
slave_sql_running: yes-----------------此状态必须yes
mysql> show slave status\g
*************************** 1
-----
slave_io_running: yes-----------------此状态必须yes
slave_sql_running: yes-----------------此状态必须yes
-----
双主搭建:
主键冲突
主键冲突
步长要不一致,防止binlog冲突
主:1
另一主:2
主:1
另一主:2
------------------------
gtid
在mysql中查看server_uuid,mysql在启动过程中,读取auto.cnf的server__uuid
在从库开启gtid,开始从主库复制数据
change master to
master_host='192.168.60.22',master_port=3306,master_user='rep',master_password='123456',master_auto_p
osition=1;
-----------------------
配置半同步复制需要先配置异步复制
加载插件
主库和从库都需要安装插件
主库
install plugin rpl_semi_sync_master soname 'semisync_master.so';
从库
install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
查看插件是否加载成功
show plugins;
rpl_semi_sync_master | active | replication | semisync_master.so | gpl
启动半同步复制
在安装完插件后,半同步复制默认是关闭的,这时需设置参数来开启半同步
主库
set global rpl_semi_sync_master_enabled = 1;
从库
set global rpl_semi_sync_slave_enabled = 1;
以上的启动?式是在命令?操作,也可写在配置?件中。
在有的?可?架构下,master和slave需同时启动,以便在切换后能继续使?半同步复制
plugin-load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl-semi-sync-master-enabled = 1
rpl-semi-sync-slave-enabled = 1
重启从库的io线程
stop slave io_thread;
start slave io_thread;
如果没有重启,则默认还是异步复制,重启后,slave会在master上注册为半同步复制的slave??。
这时候,主的error.log中会打印如下信息:
2017-04-19 11:09:26 28300 [note] semi-sync replication switched on with slave (server_id: 28703307)
at (mysql-bin.000002, 510)
2017-04-19 11:09:26 28300 [note] start semi-sync binlog_dump to slave (server_id: 28703307),
pos(mysql-bin.000002, 510)
2017-04-19 11:09:26 28300 [note] stop asynchronous binlog_dump to slave (server_id: 28703307)
查看半同步是否在运?
主库
mysql> show status like 'rpl_semi_sync_master_status';
+-----------------------------+-------+
| variable_name | value |
+-----------------------------+-------+
| rpl_semi_sync_master_status | on |
+-----------------------------+-------+
1 row in set (0.00 sec)
从库
mysql> show status like 'rpl_semi_sync_slave_status';
+----------------------------+-------+
| variable_name | value |
+----------------------------+-------+
| rpl_semi_sync_slave_status | on |
+----------------------------+-------+
1 row in set (0.20 sec)
这两个变量常?来监控主从是否运?在半同步复制模式下。
mysql> show status like '%semi%';
+--------------------------------------------+-------+
| variable_name | value |
+--------------------------------------------+-------+
| rpl_semi_sync_master_clients | 1 |
| rpl_semi_sync_master_net_avg_wait_time | 782 |
| rpl_semi_sync_master_net_wait_time | 3130 |
| rpl_semi_sync_master_net_waits | 4 |
| rpl_semi_sync_master_no_times | 1 |
| rpl_semi_sync_master_no_tx | 6 |
| rpl_semi_sync_master_status | on |
| rpl_semi_sync_master_timefunc_failures | 0 |
| rpl_semi_sync_master_tx_avg_wait_time | 717 |
| rpl_semi_sync_master_tx_wait_time | 2871 |
| rpl_semi_sync_master_tx_waits | 4 |
| rpl_semi_sync_master_wait_pos_backtraverse | 0 |
| rpl_semi_sync_master_wait_sessions | 0 |
| rpl_semi_sync_master_yes_tx | 4 |
+--------------------------------------------+———+
rpl_semi_sync_master_status:指示主服务器使?的是异步复制模式,还是半同步复制模式。
rpl_semi_sync_master_clients:显示有多少个从服务器配置成了半同步复制模式。
rpl_semi_sync_master_yes_tx:显示从服务器确认的成功提交数量。
rpl_semi_sync_master_no_tx:显示从服务器确认的不成功提交数量。
配置半同步复制需要先配置异步复制
加载插件
主库和从库都需要安装插件
主库
install plugin rpl_semi_sync_master soname 'semisync_master.so';
从库
install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
查看插件是否加载成功
show plugins;
rpl_semi_sync_master | active | replication | semisync_master.so | gpl
启动半同步复制
在安装完插件后,半同步复制默认是关闭的,这时需设置参数来开启半同步
主库
set global rpl_semi_sync_master_enabled = 1;
从库
set global rpl_semi_sync_slave_enabled = 1;
以上的启动?式是在命令?操作,也可写在配置?件中。
在有的?可?架构下,master和slave需同时启动,以便在切换后能继续使?半同步复制
plugin-load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl-semi-sync-master-enabled = 1
rpl-semi-sync-slave-enabled = 1
重启从库的io线程
stop slave io_thread;
start slave io_thread;
如果没有重启,则默认还是异步复制,重启后,slave会在master上注册为半同步复制的slave??。
这时候,主的error.log中会打印如下信息:
2017-04-19 11:09:26 28300 [note] semi-sync replication switched on with slave (server_id: 28703307)
at (mysql-bin.000002, 510)
2017-04-19 11:09:26 28300 [note] start semi-sync binlog_dump to slave (server_id: 28703307),
pos(mysql-bin.000002, 510)
2017-04-19 11:09:26 28300 [note] stop asynchronous binlog_dump to slave (server_id: 28703307)
查看半同步是否在运?
主库
mysql> show status like 'rpl_semi_sync_master_status';
+-----------------------------+-------+
| variable_name | value |
+-----------------------------+-------+
| rpl_semi_sync_master_status | on |
+-----------------------------+-------+
1 row in set (0.00 sec)
从库
mysql> show status like 'rpl_semi_sync_slave_status';
+----------------------------+-------+
| variable_name | value |
+----------------------------+-------+
| rpl_semi_sync_slave_status | on |
+----------------------------+-------+
1 row in set (0.20 sec)
这两个变量常?来监控主从是否运?在半同步复制模式下。
mysql> show status like '%semi%';
+--------------------------------------------+-------+
| variable_name | value |
+--------------------------------------------+-------+
| rpl_semi_sync_master_clients | 1 |
| rpl_semi_sync_master_net_avg_wait_time | 782 |
| rpl_semi_sync_master_net_wait_time | 3130 |
| rpl_semi_sync_master_net_waits | 4 |
| rpl_semi_sync_master_no_times | 1 |
| rpl_semi_sync_master_no_tx | 6 |
| rpl_semi_sync_master_status | on |
| rpl_semi_sync_master_timefunc_failures | 0 |
| rpl_semi_sync_master_tx_avg_wait_time | 717 |
| rpl_semi_sync_master_tx_wait_time | 2871 |
| rpl_semi_sync_master_tx_waits | 4 |
| rpl_semi_sync_master_wait_pos_backtraverse | 0 |
| rpl_semi_sync_master_wait_sessions | 0 |
| rpl_semi_sync_master_yes_tx | 4 |
+--------------------------------------------+———+
rpl_semi_sync_master_status:指示主服务器使?的是异步复制模式,还是半同步复制模式。
rpl_semi_sync_master_clients:显示有多少个从服务器配置成了半同步复制模式。
rpl_semi_sync_master_yes_tx:显示从服务器确认的成功提交数量。
rpl_semi_sync_master_no_tx:显示从服务器确认的不成功提交数量。