1、先安装mysql的yum源rpm -ivh mysql-community-release-el7-5.noarch.rpm
2、再安装mysql server端yum -y install mysql-community-server
3、启动mysqlsystemctl enable mysqldsystemctl start mysqld
4、安装完成之后需要进行套趱咝鲞初始化,我这里直接写了一个shell的函数进行初始化,可以复制后直接调用。也可以手动初始化Initialization_Mysql(){if [ ! -f /usr/b足毂忍珩in/expect ]; thenyum -y install expectfiexpect<<- ENDspawn mysql_secure_installationexpect "Enter current password for root (enter for none):"send "\n"expect "Set root password?"send "y\n"expect "New password:"send "123456\n"expect "Re-enter new password:"send "123456\n"expect "Remove anonymous users?"send "y\n"expect "Disallow root login remotely?"send "y\n"expect "Remove test database and access to it?"send "y\n"expect "Reload privilege tables now?"send "y\n"expect eofexitEND}
5、编辑主库、从库上的配置文件/etc/my.cnf主库在[mysqld]下增加server-id = 1log幻腾寂埒-bin= binlogbinlog_format=mixed从库在[mysqld]下增加server-id=2log-bin= binlogbinlog_format=mixedreplicate_wild_do_table=oldboy.%replicate_wild_ignore_table=mysql.%
6、重启主库、从库mysql服务使配置生效systemctl restart mysqld
7、在主库上创建slave用户,并指定该用户只能在主机192.168.137.128上登录mysql>grant replication slave on *.* to 'admin'@'192.168.137.128' identified by '123456';
8、在从库192.168.137.128上登录到主库192.168.137.139mysql -u admin -h 192.168.137.139 -p
9、在主库上查状态select user,host from mysql.user;
10、锁主库的表并查询master的状态值,供步骤14使用flush tables with read lock;show master status;
11、在主库上备份adservermysqldump --master-data -u root -p oldboy> oldboy.sql并将oldboy.sql 通过scp拷贝到从库上
12、在从库上创建oldboymysql> create database old水瑞侮瑜boy;Query 晦倘佳鳎OK, 1 row affected (0.00 sec)[root@master ~]# mysql -u root -p oldboy< /root/oldboy.sqlEnter password:[root@master ~]#
13、解锁主库表unlock tables;
14、在从库上执行mysql> stop slave;Query OK, 0 ro嘛术铹砾ws affected, 1 warning (0.00 se艘早祓胂c)mysql> change master to master_host='192.168.137.139',master_user='admin',master_password='123456',master_log_file='binlog.000001', master_log_pos=333;Query OK, 0 rows affected, 2 warnings (0.04 sec)mysql> start slave;Query OK, 0 rows affected (0.00 sec)
15、从库上查看slave status 状态mysql>show slave status\G;
16、mysql主从同步搭建完成主库insert,从库可以select