一、介绍
Mysql主从复制,前段时间生产环境部署了一套主从复制的架构,当时现找了很多资料,现在记录下
二、拓扑图
三、环境以及软件版本
主机名 | IP | 操作系统 | 角色 | 软件版本 |
MysqlDB_Master | 192.168.0.1 | CentOS release 7.1 | Master | Mysql 5.6.36 |
MysqlDB_Slave | 192.168.0.2 | CentOS release 7.1 | Slave | Mysql 5.6.36 |
四、源码安装
之前都是yum直接安装的,这次尝试使用源码安装,安装包如下:
- mysql-5.6.36.tar.gz
- cmake-3.8.1.tar.gz
首先安装前提环境:
[[email protected]_Master soft] yum groupinstall ‘Development tools‘ -y [[email protected]_Master soft] tar -xf cmake-3.8.1.tar.gz [[email protected]_Master soft] cd cmake-3.8.1 [[email protected]_Master cmake-3.8.1] ./bootstrap [[email protected]_Master cmake-3.8.1] gmake && gmke install
安装mysql:
[[email protected]_Master soft] tar -xf mysql-5.6.36.tar.gz [[email protected]_Master soft] cd mysql-5.6.36 [[email protected]_Master mysql-5.6.36] cmake -DCMAKE_INSTALL_PREFIX=/data/opt/mysql -DMYSQL_DATADIR=/data/opt/mysql/data -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DENABLED_LOCAL_INFILE=1 [[email protected]_Master mysql-5.6.36] make && make install
mysql配置相关:
[[email protected]_Master mysql-5.6.36] mkdir -p /data/opt/mysql/data/{3306,binlog,tmp} [[email protected]_Master mysql-5.6.36] chown -R mysql.mysql /data/opt/mysql [[email protected]_Master mysql-5.6.36] cp support-files/my-default.cnf /etc/my.conf [[email protected]_Master mysql-5.6.36] vim /etc/my.conf =================================== [client] port = 3306 socket = /data/opt/mysql/3306/mysql.sock #The MySQL Server [mysqld] server-id=209 innodb_flush_log_at_trx_commit=1 sync_binlog=1 port = 3306 user = mysql socket = /data/opt/mysql/3306/mysql.sock pid-file = /data/opt/mysql/3306/mysql.pid basedir = /data/opt/mysql datadir = /data/opt/mysql/data tmpdir = /data/opt/mysql/tmp open_files_limit = 10240 lower_case_table_names=1 explicit_defaults_for_timestamp sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES #Buffer max_allowed_packet = 256M max_heap_table_size = 256M net_buffer_length = 8k sort_buffer_size = 2M join_buffer_size = 4M read_buffer_size = 2M read_rnd_buffer_size = 16M #Log log-bin = /data/opt/mysql/binlog/mysql-bin binlog_cache_size = 32M max_binlog_cache_size = 512M max_binlog_size = 512M binlog_format = mixed log_output = FILE log-error = /data/opt/mysql/binlog/mysql-error.log slow_query_log = 1 slow_query_log_file = /data/opt/mysql/binlog/slow_query.log general_log = 0 general_log_file = /data/opt/mysql/binlog/general_query.log expire-logs-days = 14 #InnoDB innodb_data_file_path = ibdata1:2048M:autoextend innodb_log_file_size = 256M innodb_log_files_in_group = 3 innodb_buffer_pool_size = 1024M [mysql] no-auto-rehash prompt = (\[email protected]\h)[\d]>\_ default-character-set = gbk =================================== #添加mysql环境变量 [[email protected]_Master mysql-5.6.36] echo ‘export PATH=$PATH:/data/opt/mysql/bin‘ >> /etc/profile && source /etc/profile #初始化数据库 [[email protected]_Master mysql-5.6.36] cd /data/opt/mysql [[email protected]_Master mysql] ./scripts/mysql_install_db --defults-file=/etc/my.cnf --user=mysql [[email protected]_Master mysql] ./bin/mysqld_safe --user=mysql & #添加service启动 [[email protected]_Master mysql] cp /soft/mysql-5.6.36/support-files/mysql.server /etc/init.d/mysqld [[email protected]_Master mysql] chmod +x /ect/init.d/mysqld
优化mysql默认配置,配置访问权限:
[[email protected]_Master ~] mysql -uroot -p #清空默认数据 ([email protected])[(none)] > select * from mysql.db \G ([email protected])[(none)] > truncate table mysql.db; ([email protected])[(none)] > flush privileges; ([email protected]calhost)[(none)] > select * from mysql.db \G #配置访问权限 ([email protected])[(none)] > use mysql; ([email protected])[(none)] > desc user; ([email protected])[(none)] > grant all privileges on *.* to root@"%" identified by "root"; ([email protected])[(none)] > update user set Password=password(‘XXXXXX‘) where User=‘root‘; ([email protected])[(none)] > select Host,User,Password from user where User=‘root‘; ([email protected])[(none)] > flush privileges; ([email protected])[(none)] > exit;
以上,Mysql源码安装完成,在MysqlDB_Slave服务器上进行相同配置即可。
五、配置主从
关于主从的配置,之前在配置文件里已经填写,“server-id”值主从不同,从值大于主值,主要配置如下:
[mysqld] server-id=209 #InnoDB innodb_flush_log_at_trx_commit=1 sync_binlog=1
连接到主库,配置同步账号并授权从服务器连接:
([email protected])[(none)] grant replication slave,reload,super on *.* to ‘repl‘@‘192.168.0.2‘ identified by ‘backup‘; ([email protected])[(none)] flush tables with read lock; ([email protected])[(none)] show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000007 | 321| | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
连接到从库,配置主服务器IP以及同步账号等信息:
([email protected])[(none)] > change master to master_host=‘192.168.0.1‘,master_user=‘repl‘,master_password=‘backup‘,master_log_file=‘mysql-bin.000007‘,master_log_pos=321; ([email protected])[(none)] > start slave; ([email protected])[(none)] > show slave status \G
确认主从节点同步正常,主要确认以下值:
Master_Log_File: mysql-bin.000007 Relay_Master_Log_File: mysql-bin.000007 Slave_IO_Running: Yes Slave_SQL_Running: Yes
连接到主库,解除锁,并创建新DB:
([email protected])[(none)] unlock tables; ([email protected])[(none)] create database testDB;
连接到从库,查看新建DB是否同步过来:
([email protected])[(none)] show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | testDB | +--------------------+
以上,Mysql集群,主从同步配置完成。
时间: 2024-12-17 19:11:10