mysql服务搭建+主从复制

配置yum

cd /etc/yum.repos.d/ && mkdir yum && mv *.repo yum && cp yum/CentOS-Media.repo . && vim CentOS-Media.repo

关闭防火墙 + selinux

service iptables stop && chkconfig iptables off && setenforce 0

所需要的软件(主从复制+读写分离)

cp cmake-2.8.6.tar.gz 

mysql-5.5.22.tar.gz 

amoeba-mysql-binary-2.2.0.tar.gz 

jdk-6u14-linux-x64.bin /root


在master(主) slave1(从1) slave2(从2) 上安装mysql数据库

安装准备

yum install -y ncurses-devel

解压安装cmake

tar -zxf cmake-2.8.6.tar.gz && tar -zxf mysql-5.5.22.tar.gz 

cd cmake-2.8.6 && ls && ./configure 

gmake && gmake install

编译安装mysql-5.5.22.tar.gz

tar -zxf mysql-5.5.22.tar.gz && cd mysql-5.5.22

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc && make && make install

建立配置文件

[root@CentOS6-node1 mysql-5.5.22]# cp support-files/my-medium.cnf /etc/my.cnf 

[root@CentOS6-node1 mysql-5.5.22]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld

chmod +x /etc/rc.d/init.d/mysqld 


[root@CentOS6-node1 mysql-5.5.22]# chkconfig --add mysqld

[root@CentOS6-node1 mysql-5.5.22]# chkconfig --list mysqld

mysqld         0:关闭1:关闭2:启用3:启用4:启用5:启用6:关闭


[root@CentOS6-node1 mysql-5.5.22]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile



[root@CentOS6-node1 mysql-5.5.22]# . /etc/profile


[root@CentOS6-node1 mysql-5.5.22]# groupadd mysql && useradd -M -s /sbin/nologin mysql -g mysql 

[root@CentOS6-node1 mysql-5.5.22]# chown -R mysql:mysql /usr/local/mysql/ && ls -l /usr/local/mysql/


/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql



[root@CentOS6-node1 mysql-5.5.22]# service mysqld start

Starting MySQL..                                           [确定]

[root@CentOS6-node1 mysql-5.5.22]# chkconfig mysqld on

[root@CentOS6-node1 mysql-5.5.22]# mysqladmin -u root password '666666'

[root@CentOS6-node1 mysql-5.5.22]# mysql -u root -p 

Enter password: 


mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| test               |

+--------------------+

4 rows in set (0.00 sec)



2.配置mysql master 服务器

 vim /etc/my.cnf


57 server-id       = 11    修改

49 log-bin=master-bin          修改

50 log-slave-updates=true     添加


[root@CentOS6-node1 ~]# service mysqld restart

Shutting down MySQL.                                       [确定]

Starting MySQL..                                           [确定]


 

mysql -u root -p 


mysql> GRANT REPLICATION SLAVE ON *.* TO 'myslave'@'192.168.236.%' IDENTIFIED BY '666666';


mysql> FLUSH PRIVILEGES;

Query OK, 0 rows affected (0.00 sec)


mysql> show master status;

+-------------------+----------+--------------+------------------+

| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+-------------------+----------+--------------+------------------+

| master-bin.000001 |      339 |              |                  |

+-------------------+----------+--------------+------------------+

1 row in set (0.00 sec)




配置从服务器 (server-id 不能与主服务器相同)


vim /etc/my.cnf 


57 server-id       = 22

58 relay-log=relay-log-bin

59 relay-log-index=slave-relay-bin.index


[root@CentOS6-node1 ~]# service mysqld restart

Shutting down MySQL.                                       [确定]

Starting MySQL..                                           [确定]


mysql -u root -p


mysql> change master to master_host='192.168.236.111',master_user='myslave',master_password='666666',master_log_file='master-bin.000001',master_log_pos=339;



mysql> start slave;

Query OK, 0 rows affected (0.00 sec)


mysql> show slave status\G;

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.236.111

                  Master_User: myslave

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: master-bin.000001

          Read_Master_Log_Pos: 339

               Relay_Log_File: relay-log-bin.000002

                Relay_Log_Pos: 254

        Relay_Master_Log_File: master-bin.000001

             

             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: 339

              Relay_Log_Space: 408

              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: 11

1 row in set (0.00 sec)


ERROR: 

No query specified


(1)验证主从复制的效果


1.mysql -u root -p


show databases;


所看到的库都是相同的


2.在主服务器上新建个库

mysql> create database db_test;

Query OK, 1 row affected (0.00 sec)


然后从服务器上查看 结果一样 


成功!!!!

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| test               |

+--------------------+

4 rows in set (0.00 sec)


mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| db_test            |

| mysql              |

| performance_schema |

| test               |

+--------------------+

5 rows in set (0.00 sec)


本文出自 “浅笑习惯囿你” 博客,谢绝转载!

你可能感兴趣的:(mysql服务搭建+主从复制)