码迷,mamicode.com
首页 > 数据库 > 详细

48.MYSQL的主从与读写分离

时间:2017-07-10 16:44:19      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:linux

linux企业级知识点总结

Mysql主从复制与读写分离原理图 

Mysql的主从复制:从服务器在主服务器上自动同步数据。

Mysql的读写分离:通过amoeba服务器控制用户读数据在从服务器上进行,写数据从主服务器上进行,这样可以做到负载均衡。


 

搭建mysql主从复制:

建立时间同步环境

#yum -y install ntp

#vim /etc/ntp.conf

修改相关内容:

Server  127.127.1.0

Fudge   127.127.1.0  stratum  8

#service ntpd restart

#yum -y install ntpdate

#/usr/sbin/ntpdate  1.1.1.1

安装mysql数据库

# yum -y install ncurses-devel

# yum -y install cmake

# tar xzvf 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

#useradd  mysql  -s  /sbin/nologin  -M  -g  mysql

# chown -R mysql.mysql  /usr/local/mysql/

# cp /usr/local/mysql/support-files/my-medium.cnf  /etc/my.cnf

# cd /usr/local/mysql/scripts/

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

# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile

# . /etc/profile

# cp /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysqld

# chkconfig --add mysqld

# chkconfig mysqld on

#service mysqld start

# mysqladmin -u root password  123

修改master主服务器配置文件

#vim /etc/my.cnf

server-id  =  11

log-bin=master-bin

log-slave-updates=true

重启服务

#Service mysqld restart

登陆mysql数据库,给从服务器授权

#mysql -u root -p123

Mysql>GRANT REPLICATION SLAVE ON *.* TO ren@1.1.1.% IDENTIFIED BY 123;

Mysql>FLUSH PRIVILEGES;

Mysql>show master status;

注:*.*表示所有数据库下所有表

 

配置从服务器

#vim /etc/my.cnf

修改内容:

Server-id = 22

Relay-log=relay-log-bin

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

注:这里的ID号不能与主服务器相同

重启服务

#service  mysqld restart

登陆mysql配置同步

#mysql -u root -p123,

Mysql>change master to master_host=1.1.1.1,master_user=ren ,master_password=123,

 master_log_file=master-bin.000001,master_log_pos=339

启动同步

Mysql>start slave;

Mysql>show slave status\G;

验证方法:在主服务器上创建一个新的数据库,在从服务器上查看是否同步过来。

Mysql的读写分离

搭建amoeba服务器

安装JAVA环境

# chmod +x jdk-6u14-linux-x64.bin

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

# mv jdk1.6.0_14/  /usr/local/jdk1.6

#vim /etc/profile

添加内容:

export JAVA_HOME=/usr/local/jdk1.6

export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib

export PATH=$JAVA_HOME/lib:$JAVA_HOME/jre/bin:$PATH:$HOME/bin

export AMOEBA_HOME=/usr/local/amoeba/

export PATH=$PATH:$AMOEBA_HOME/bin

#source /etc/profile

#java -version

安装amoeba软件

#mkdir /usr/local/amoeba

#tar xzvf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba

#chmod -R  755  /usr/local/amoeba

配置masterslave1salve2服务器开放权限给amoeba访问

Grant  all on *.* to test@1.1.1.% identified by 123;

编辑配置amoeba.xml配置文件,指定amoeba的用户名和密码,指定读写服务器。

#vim /usr/local/amoeba/conf/amoeba.xml

                        <property name="authenticator">

                                <bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">

                                      <property name="user">amoeba</property>

 

                                        <property name="password">123</property>

                                        <property name="filter">

                                                <bean class="com.meidusa.amoeba.server.IPAccessController">

                                                        <property name="ipFile">${amoeba.home}/conf/access_list.conf</property>

                                                </bean>

                                        </property>

                <property name="defaultPool">master</property>

                <property name="writePool">master</property>

                <property name="readPool">slaves</property>

编辑配置dbservers.xml配置文件,指定访问的数据库的账号和密码,指定主从服务器

#vim /usr/local/amoeba/conf/dbServers.xml

                        <!-- mysql user -->

                        <property name="user">test</property>

                        <property name="password">123</property>

        <dbServer name="master"  parent="abstractServer">

                <factoryConfig>

                        <!-- mysql ip -->

                        <property name="ipAddress">1.1.1.1</property>

                </factoryConfig>

        </dbServer>

        <dbServer name="slave2"  parent="abstractServer">

                <factoryConfig>

                        <!-- mysql ip -->

                        <property name="ipAddress">1.1.1.2</property>

                </factoryConfig>

        </dbServer>

        <dbServer name="slaves" virtual="true">

                <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">

                        <!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->

                        <property name="loadbalance">1</property>

                        <!-- Separated by commas,such as: server1,server2,server1 -->

                        <property name="poolNames">slave1,slave2</property>

                </poolConfig>

        </dbServer>

启动amoeba软件

#/usr/local/amoeba/bin/amoeba start

验证方法:master服务器上写入数据同步到slave读服务器上,然后slave读服务器关闭同步,自己增加相关表中内容,然后使用客户端连接测试是否能成功在slacve服务器上读取内容。

关闭同步:Mysql>stop  slave;

 


48.MYSQL的主从与读写分离

标签:linux

原文地址:http://10978134.blog.51cto.com/10968134/1945785

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!