mysql双机热备安装一、安装mysql#tar -xf mysql-5.7.18-1.el6.x86_64.rpm-bundle.tar #yum localinstall *.rpm
1.1修改mysql配置# for advice on how to change settings please see# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html[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 = 2mdatadir=/data/mysqlsocket=/var/lib/mysql/mysql.sock# disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0log-error=/var/log/mysqld.logpid-file=/var/run/mysqld/mysqld.pidexplicit_defaults_for_timestamp=truetmpdir=/tmp[client]default-character-set=utf8mb4[mysqld]character_set_server=utf8mb4
1.2权限修改[root@172 ~]# chown -r mysql:mysql /data[root@172 ~]# chmod 777 -r /data/[root@172 ~]# chmod -r 777 /tmp
1.3启动mysql服务[root@172 ~]# service mysqld restartstopping mysqld: [failed]initializing mysql database: [ ok ]installing validate password plugin: [ ok ]starting mysqld: [ ok ]
1.4查看temp密码more /var/log/mysqld.log |grep temporary
1.5修改root密码db1alter user 'root'@'localhost' identified by '*****';flush privileges;exit;
db2alter user 'root'@'localhost' identified by '*****';flush privileges;exit;
二、配置主从同步master1172.28.8.187
master2 172.28.8.188
2.1 配置master1给master2登录的密码master1
create user 'repl' identified by '*****';grant replication slave on *.* to 'repl'@'172.28.8.188' identified by '*****';flush privileges;mysql> create database mydb default charset utf8;
在172.28.8.188测试repuser是否能登录172.28.8.187上的数据库mysql -urepl -p -h172.28.8.187
2.1.1 master1配置my.cnf# for advice on how to change settings please see# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html[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 = 2mdatadir=/data/mysqlsocket=/var/lib/mysql/mysql.sock# disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0log-error=/var/log/mysqld.logpid-file=/var/run/mysqld/mysqld.pidexplicit_defaults_for_timestamp=truetmpdir=/tmpcharacter_set_server=utf8mb4server-id=177log-bin=/var/log/mysql/mysql-bin.logread-only=0binlog-ignore-db=mysqlbinlog-ignore-db=information_schemaexpire_logs_days= 365auto-increment-increment = 2auto-increment-offset = 1[client]default-character-set=utf8mb4
2.2 master2配置my.cnf#除server-id外,其他与master1保持一致
2.2.1 master2给master1创建账号密码并授权create user 'repl' identified by '*****';grant replication slave on *.* to 'repl'@'172.28.8.187' identified by '*****';flush privileges;
2.3 查看master同步状态master1
mysql> show master status;+------------------+----------+--------------+--------------------------+-------------------+| file | position | binlog_do_db | binlog_ignore_db | executed_gtid_set |+------------------+----------+--------------+--------------------------+-------------------+| mysql-bin.000001 | 154 | mydb | mysql,information_schema | |+------------------+----------+--------------+--------------------------+-------------------+1 row in set (0.00 sec)
master2
mysql> show master status;+------------------+----------+--------------+--------------------------+-------------------+| file | position | binlog_do_db | binlog_ignore_db | executed_gtid_set |+------------------+----------+--------------+--------------------------+-------------------+| mysql-bin.000001 | 154 | mydb | mysql,information_schema | |+------------------+----------+--------------+--------------------------+-------------------+1 row in set (0.00 sec)
设置master1从master2同步
mysql>change master to master_host='172.28.8.188',master_port=3306,master_user='repl',master_password='b4l:ggtg3s0*',master_log_file='mysql-bin.000002',master_log_pos=860;mysql> show slave status\gmysql> start slave;mysql> show slave status\g
设置master2从master1同步
mysql>change master to master_host='172.28.8.187',master_port=3306,master_user='repl',master_password='2s1*8pr+bzqh^8t`',master_log_file='mysql-bin.000003',master_log_pos=1497;mysql> show slave status\gmysql> start slave;mysql> show slave status\g
如出现以下两项,则说明配置成功!
slave_io_running: yes slave_sql_running: yes
3.双主同步测试进入master1 mysql 数据库
mysql> create database crm;query ok, 1 row affected (0.00 sec)mysql> use crm;database changedmysql> create table employee(id int auto_increment,name varchar(10),primary key(id));query ok, 0 rows affected (0.00 sec)mysql> insert into employee(name) values('a');query ok, 1 row affected (0.00 sec)mysql> insert into employee(name) values('b');query ok, 1 row affected (0.00 sec)mysql> insert into employee(name) values('c');query ok, 1 row affected (0.06 sec)mysql> select * from employee;+----+------+| id | name |+----+------+| 1 | a || 3 | b || 5 | c |+----+------+3 rows in set (0.00 sec)
进入master2,查看是否有crm这个数据库和employee表。
mysql> show databases;+--------------------+| database |+--------------------+| information_schema || crm || mysql || performance_schema |+--------------------+4 rows in set (0.00 sec)mysql> use crm;reading table information for completion of table and column namesyou can turn off this feature to get a quicker startup with -adatabase changedmysql> show tables;+---------------+| tables_in_crm |+---------------+| employee |+---------------+1 row in set (0.00 sec)mysql> select * from employee;+----+------+| id | name |+----+------+| 1 | a || 3 | b || 5 | c |+----+------+3 rows in set (0.00 sec)mysql> insert into employee(name) values('d');query ok, 1 row affected (0.00 sec)mysql> select * from employee;+----+------+| id | name |+----+------+| 1 | a || 3 | b || 5 | c || 7 | d |+----+------+4 rows in set (0.00 sec)
在master1的中查看是否有刚刚在master2中插入的数据。
mysql> select * from employee;+----+------+| id | name |+----+------+| 1 | a || 3 | b || 5 | c || 7 | d |+----+------+4 rows in set (0.00 sec)
推荐学习:《mysql视频教程》
以上就是详解mysql双机热备安装步骤的详细内容。