前提 数据库已经安装完毕
1、主从服务器分别作以下操作:
1.1、版本一致 1.2、初始化表,并在后台启动mysql 1.3、修改root的密码
2、举栗子:
举栗子
- master: 192.168.1.1
- slave: 192.168.1.2
2.1 修改主服务器master:
vim /etc/my.cnf [client] port= 3306 socket= /tmp/mysql.sock default-character-set=utf8 [mysql] default-character-set=utf8 [mysqld] port= 3306 socket= /tmp/mysql.sock basedir= /usr/local/mysql datadir= /data/mysql open_files_limit = 3072 back_log = 103 max_connections = 800 max_connect_errors = 100000 table_open_cache = 512 external-locking = FALSE max_allowed_packet = 32M sort_buffer_size = 2M join_buffer_size = 2M thread_cache_size = 51 query_cache_size = 32M tmp_table_size = 96M max_heap_table_size = 96M slow_query_log = 1 slow_query_log_file = /log/mysql/slow.log log-error = /log/mysql/error.log long_query_time = 1 server-id = 1 log-bin=master-bin log_bin_index=master-bin.index binlog-do-db=database_A //需要同步的数据库。。 写字间的数据库名 log-bin = /log/mysql/mysql-bin sync_binlog = 1 binlog_cache_size = 128M max_binlog_cache_size = 512M max_binlog_size = 1024M expire_logs_days = 60 key_buffer_size = 32M read_buffer_size = 1M read_rnd_buffer_size = 16M bulk_insert_buffer_size = 64M character-set-server=utf8 default-storage-engine = InnoDB binlog-format = row innodb_buffer_pool_dump_at_shutdown = 1 innodb_buffer_pool_load_at_startup = 1 binlog_rows_query_log_events = 1 explicit_defaults_for_timestamp = 1 #log_slave_updates=1 #gtid_mode=on #enforce_gtid_consistency=1 #innodb_write_io_threads = 8 #innodb_read_io_threads = 8 #innodb_thread_concurrency = 0 transaction_isolation = REPEATABLE-READ innodb_additional_mem_pool_size = 16M innodb_buffer_pool_size = 512M #innodb_data_home_dir = innodb_data_file_path = ibdata1:1024M:autoextend innodb_flush_log_at_trx_commit = 1 innodb_log_buffer_size = 16M innodb_log_file_size = 512M innodb_log_files_in_group = 2 innodb_max_dirty_pages_pct = 50 innodb_file_per_table = 1 innodb_locks_unsafe_for_binlog = 0 wait_timeout = 14400 interactive_timeout = 14400 skip-name-resolve [mysqldump] quick max_allowed_packet = 32M
2.2 修改从服务器slave:
[mysqld] # # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 #禁用local_infile选项会降低攻击者通过SQL注入漏洞器读取敏感文件的能力 local-infile=0 server-id=2 relay-log=slave-relay-bin relay-log-index=slave-relay-bin.index log-bin=/var/lib/mysql/mysql-bin binlog-format=ROW # Recommended in standard MySQL setup sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
3. 重启数据库
- service mysqld restart
4.主服务器上建立账户并授权slave
mysql>GRANT REPLICATION SLAVE ON *.* to 'mysync'@'%' identified by 'q123456'; //一般不用root帐号
“%”表示所有客户端都可能连,只要帐号,密码正确,此处可用具体客户端IP代替,如192.168.145.226,加强安全。
登录主服务器的mysql,查询master的状态
mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000005 | 822410 | izone | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.01 sec)
注:执行完此步骤后不要再操作主服务器MYSQL,防止主服务器状态值变化
5. 配置从服务器Slave:
//最优写法 master_log_pos默认值0 change master to master_host='192.168.1.1', master_user='mysync', master_password='q123456', master_log_file='mysql-bin.000005', master_log_pos=0; mysql>change master to master_host='192.168.1.1',master_user='mysync',master_password='q123456', master_log_file='mysql-bin.000005',master_log_pos=822410; //注意不要断开,822410数字前后无单引号。 Mysql>start slave; //启动从服务器复制功能
6. 检查从服务器复制功能状态:
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.1 Master_User: slave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000005 Read_Master_Log_Pos: 822410 Relay_Log_File: slave-relay-bin.000604 Relay_Log_Pos: 4962 Relay_Master_Log_File: mysql-bin.000005 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 822410 Relay_Log_Space: 5298 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_UUID: 4ba711e6-1e80-11eb-b46a-00163e002678 Master_Info_File: /var/lib/mysql/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 1 row in set (0.00 sec) mysql>
注:Slave_IO及Slave_SQL进程必须正常运行,即YES状态,否则都是错误的状态(如:其中一个NO均属错误)。 以上操作过程,主从服务器配置完成。
可以新建数据库测试,也导入数据库
监控:编写一shell脚本,用nagios监控slave的两个yes(Slave_IO及Slave_SQL进程),如发现只有一个或零个yes,就表明主从有问题
版权声明:如无特殊说明,文章均为本站原创,转载请注明出处
本文链接:http://kkxl95.cn/article/1628064237/
学长 [博主]
1 楼 - 3 年,5月前
有道理。。读写分离的一些问题,写代码的同学也不要坑了运维同学读写分离的一些些问题