温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

48.MYSQL的主从与读写分离

发布时间:2020-05-28 14:04:54 来源:网络 阅读:280 作者:乘_风 栏目:建站服务器

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;

 


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI