mysql架构精选

◆主从架构
1.安装服务(主从)
yum -y install mysql*
/etc/init.d/mysqld start
2.修改配置文件:/etc/my.conf(主从)
vi /etc/my.conf
log-bin=mysql-bin ###开启二进制日志文件
server-id=131 ###声明服务器ID,不重复即可
/etc/init.d/mysqld restart
3.1进入主—数据库
mysqladmin -uroot password '123'
mysql -uroot –p
##创建从服务器登录用户
##grant replication slave on *.* to '用户'@'%' identified by '密码';
grant replication slave on *.* to 'master'@'%' identified by '123';
show master status; ##查看二进制文件名,以及大小
3.2进入从-数据库
mysqladmin -uroot password '123'
mysql -uroot –p
##确定从主服务器同步
##change master to master_host='主服务器IP',master_user='用户',master_password='密码',master_log_file='二进制文件',master_log_pos=文件空间大小;
change master to master_host='192.168.116.131',master_user='master',master_password='123',master_log_file='mysql-bin.000001',master_log_pos=385;
##启动同步
start slave;
##查看同步服务开启状态
show slave status\G;
4.测试
主:create database aa;
从:show databases;
◆主从从架构
1.安装服务(主从)
yum -y install mysql*
/etc/init.d/mysqld start
2.修改配置文件:/etc/my.conf(主从)
vi /etc/my.conf
log-bin=mysql-bin ###开启二进制日志文件
server-id=131 ###声明服务器ID,不重复即可
/etc/init.d/mysqld restart
3.1进入主—数据库
mysqladmin -uroot password '123'
mysql -uroot –p
##创建从服务器登录用户
##grant replication slave on *.* to '用户'@'%' identified by '密码';
grant replication slave on *.* to 'master'@'%' identified by '123';
show master status; ##查看二进制文件名,以及大小
3.2进入从1-数据库
mysqladmin -uroot password '123'
mysql -uroot –p
##确定从主服务器同步
##change master to master_host='主服务器IP',master_user='用户',master_password='密码',master_log_file='二进制文件',master_log_pos=文件空间大小;
change master to master_host='192.168.116.131',master_user='master',master_password='123',master_log_file='mysql-bin.000001',master_log_pos=385;
##启动同步
start slave;
##查看同步服务开启状态
show slave status\G;
3.3进入从2-数据库
mysqladmin -uroot password '123'
mysql -uroot –p
##确定从主服务器同步
##change master to master_host='主服务器IP',master_user='用户',master_password='密码',master_log_file='二进制文件',master_log_pos=文件空间大小;
change master to master_host='192.168.116.131',master_user='master',master_password='123',master_log_file='mysql-bin.000001',master_log_pos=385;
##启动同步
start slave;
##查看同步服务开启状态
show slave status\G;
4.测试
主:create database bb;
从1:show databases;
从2:show databases;
◆主主架构(互为主从)
###服务器有数据的情况,需要手动先同步一致,cp重定向到mysql
1.安装服务(主主)
yum -y install mysql*
/etc/init.d/mysqld start
2.修改配置文件:/etc/my.conf(主主)
vi /etc/my.conf
log-bin=mysql-bin ###开启二进制日志文件
server-id=131 ###声明服务器ID,不重复即可
###下面几个看实际情况使用,实验环境可忽略
replicate-do-db=test ###需要同步的数据库
binlog-ignore-db=mysql ###不需要同步的数据库
binlog-ignore-db=information_schema ##不需要同步的数据库
auto-increment-increment=2 ###为了保证含有自增长的不会冲突
auto-increment-offset=1 ###为了保证含有自增长的不会冲突
log-slave-updates=true
/etc/init.d/mysqld restart
3.1进入主1—数据库 ip:192.168.116.131
mysqladmin -uroot password '123'
mysql -uroot –p
##创建从服务器登录用户
##grant replication slave on *.* to '用户'@'%' identified by '密码';
grant replication slave on *.* to 'master'@'%' identified by '123';
flush privileges;
show master status; ##查看二进制文件名,以及大小
##确定从主服务器同步
##change master to master_host='主服务器IP',master_user='用户',master_password='密码',master_log_file='二进制文件',master_log_pos=文件空间大小;
change master to master_host='192.168.116.132',master_user='slave',master_password='123',master_log_file='mysql-bin.000001',master_log_pos=384;
##启动同步
start slave;
##查看同步服务开启状态
show slave status\G;
3.2进入主2-数据库 ip:192.168.116.132
mysqladmin -uroot password '123'
mysql -uroot –p
##创建从服务器登录用户
##grant replication slave on *.* to '用户'@'%' identified by '密码';
grant replication slave on *.* to 'slave'@'%' identified by '123';
show master status; ##查看二进制文件名,以及大小
##确定从主服务器同步
##change master to master_host='主服务器IP',master_user='用户',master_password='密码',master_log_file='二进制文件',master_log_pos=文件空间大小;
change master to master_host='192.168.116.131',master_user='master',master_password='123',master_log_file='mysql-bin.000001',master_log_pos=247;
##启动同步
start slave;
##查看同步服务开启状态
show slave status\G;
4.测试
主1:create database aa;show databases;主2:show databases;
主2:create database aa;show databases;主1:show databases;
◆主主从架构
1.安装服务(主主从)
yum -y install mysql*
/etc/init.d/mysqld start
2.主配置文件:/etc/my.conf
2.1两台主服务器
vi /etc/my.conf
log-bin=mysql-bin ###开启二进制日志文件
server-id=131和132 ###声明服务器ID,不重复即可
/etc/init.d/mysqld restart
2.2从服务器
vi /etc/my.cnf
[mysqld_multi]
mysqld=/usr/bin/mysqld_safe
mysqladmin=/usr/bin/mysqladmin
log=/tmp/multi.log
[mysqld131]
port=3306
pid-file=/var/lib/mysqla/mysqld.pid
datadir=/var/lib/mysqla
socket=/var/lib/mysqla/mysql.sock
server-id=129
user=mysql
[mysqld132]
port=3307
pid-file=/var/lib/mysqlb/mysqld.pid
datadir=/var/lib/mysqlb
socket=/var/lib/mysqlb/mysql.sock
server-id=129
user=mysql
3.1 主1进入数据库
mysqladmin -uroot password '123'
mysql -uroot -p
grant replication slave on *.* to 'master1'@'%' identified by '123';
show master status;
3.2 主2进入数据库
mysqladmin -uroot password '123'
mysql -uroot -p
grant replication slave on *.* to 'master2'@'%' identified by '123';
show master status;
3.3 从服务器
mysql_install_db --datadir=/var/lib/mysqla --user=mysql ###初始化mysqla数据库
mysql_install_db --datadir=/var/lib/mysqlb --user=mysql ###初始化mysqlb数据库
cd /var/lib
chown -R mysql /var/lib/mysqla/ ##修改文件权限
chown -R mysql /var/lib/mysqlb/ ##修改文件权限
mysqld_multi --defaults-file=/etc/my.cnf start 131 ##主1id号,开起数据库01,为主1备份
mysqld_multi --defaults-file=/etc/my.cnf start 132 ##主2id号,开起数据库02,为主2备份
测试端口:netstat -an |grep 3306&3307
mysqladmin -u root password '123' -S /var/lib/mysqla/mysql.sock ##建立131数据库密码
mysqladmin -u root password '123' -S /var/lib/mysqlb/mysql.sock ##建立132数据库密码

####进入mysqla ----131数据库配置
mysql -uroot -p -S /var/lib/mysqla/mysql.sock
change master to master_host='192.168.116.131',master_user='master1',master_password='123',master_log_file='mysql-bin.000001',master_log_pos=247;
start slave;
show slave status\G;
exit

####进入mysqlb -----132数据库配置
mysql -uroot -p -S /var/lib/mysqlb/mysql.sock
change master to master_host='192.168.116.132',master_user='master2',master_password='123',master_log_file='mysql-bin.000001',master_log_pos=247;
start slave;
show slave status\G;
exit
4.测试
主1备份测试:
主1: create database aa;
从:
mysql -uroot -p -S /var/lib/mysqla/mysql.sock
show databases;
主2备份测试:
主2:create database bb;
从:
mysql -uroot -p -S /var/lib/mysqlb/mysql.sock
show databases;


◆mysql读写分离 -----amoeba
1.两台数据库主从关系
yum -y install mysql*
/etc/init.d/mysqld start
vi /etc/my.conf
log-bin=mysql-bin ###开启二进制日志文件
server-id=131 ###声明服务器ID,不重复即可
/etc/init.d/mysqld restart
##主数据库
mysqladmin -uroot password '123'
mysql -uroot –p
grant all privileges on *.* to 'amoeba'@'%' identified by '123';
show master status; ##查看二进制文件名,以及大小

###从数据库
mysqladmin -uroot password '123'
mysql -uroot –p
change master to master_host='192.168.116.131',master_user='amoeba',master_password='123',master_log_file='mysql-bin.000001',master_log_pos=382;
grant all privileges on *.* to 'amoeba'@'%' identified by '123';

##查看同步服务开启状态
show slave status\G;
#启动同步
start slave;
###测试
主:create database aa;
从:show databases;


2.配置amobe服务器
yum -y install lrzsz mysql
rz amoeba-n.iso
mkdir /iso
mount -o loop amoeba-n.iso /iso/
cp /iso/* .
tar zxvf jdk-7u40-linux-x64.gz
mv jdk1.7.0_40/ /usr/local/jdk
vi /etc/profile
#JDK
JAVA_HOME=//usr/local/jdk
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$CLASSPATH export CLASSPATH
source /etc/profile

###测试:echo $PATH java -version

unzip amoeba-mysql-1.3.1-BETA.zip -d /usr/local/amoeba
chmod -R a+x /usr/local/amoeba/bin/
vi /usr/local/amoeba/conf/amoeba.xml
#####amoeba主配置信息
<?xml version="1.0" encoding="gbk"?>
<!DOCTYPE amoeba:configuration SYSTEM "amoeba.dtd">
<amoeba:configuration xmlns:amoeba="http://amoeba.meidusa.com/">

<server>
<!-- proxy server°ó¶¨µÄ¶Ë¿Ú -->
<property name="port">9000</property>

<!-- proxy server°ó¶¨µÄIP -->
<property name="ipAddress">192.168.116.129</property>
<!-- proxy server net IO Read thread size -->
<!-- proxy server client process thread size -->
<property name="clientSideThreadPoolSize">30</property>

<!-- mysql server data packet process thread size -->
<property name="serverSideThreadPoolSize">30</property>


<!-- Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm). -->
<property name="tcpNoDelay">true</property>

<!-- ¶ÔÍâÑéÖ¤µÄÓû§Ãû -->
<property name="user">root</property>

<!-- ¶ÔÍâÑéÖ¤µÄÃÜÂë -->
<property name="password">456</property>

<!-- query timeout( default: 60 second , TimeUnit:second) -->
<property name="queryTimeout">60</property>
</server>

<!--
ÿ¸öConnectionManager¶¼½«×÷Ϊһ¸öÏß³ÌÆô¶¯¡£
manager¸ºÔðConnection IO¶Áд/ËÀÍö¼ì²â
-->
<connectionManagerList>

<!--
default value is avaliable Processors
<property name="processors">5</property>
-->
</connectionManager>
</connectionManagerList>

<dbServerList>
<!--
һ̨mysqlServer ÐèÒªÅäÖÃÒ»¸öpool£¬
Èç¹û¶ą̀ ƽµÈµÄmysqlÐèÒª½øÐÐloadBalance£¬
¼òµ¥µÄÅäÖÃÊÇÊôÐÔ¼ÓÉÏ virtual="true",¸ÃPool ²»ÔÊÐíÅäÖÃfactoryConfig
»òÕß×Ô¼ºÐ´Ò»¸öObjectPool¡£
-->
<dbServer name="server1">

<!-- PoolableObjectFactoryʵÏÖÀà -->
<property name="manager">defaultManager</property>

<!-- ÕæʵmysqlÊý¾Ý¿â¶Ë¿Ú -->
<property name="port">3306</property>

<!-- ÕæʵmysqlÊý¾Ý¿âIP -->
<property name="ipAddress">192.168.116.131</property>
<property name="schema">aa,bb</property>

<!-- ÓÃÓڵǽmysqlµÄÓû§Ãû -->
<property name="user">amoeba</property>

<!-- ÓÃÓڵǽmysqlµÄÃÜÂë -->

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

</factoryConfig>

<!-- ObjectPoolʵÏÖÀà -->
<poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
<property name="maxActive">200</property>
<property name="maxIdle">200</property>
<property name="minIdle">10</property>
<property name="minEvictableIdleTimeMillis">600000</property>
<property name="timeBetweenEvictionRunsMillis">600000</property>
<property name="testOnBorrow">true</property>
<property name="testWhileIdle">true</property>
</poolConfig>
</dbServer>


<dbServer name="master" virtual="true">
<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
<!-- ¸ºÔؾùºâ²ÎÊý 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
<property name="loadbalance">1</property>

<!-- ²ÎÓë¸Ãpool¸ºÔؾùºâµÄpoolNameÁбíÒÔ¶ººÅ·Ö¸î -->
<property name="poolNames">server1</property>
</poolConfig>
</dbServer>
<dbServer name="server2">

<!-- PoolableObjectFactoryʵÏÖÀà -->
<property name="manager">defaultManager</property>

<!-- ÕæʵmysqlÊý¾Ý¿â¶Ë¿Ú -->
<property name="port">3306</property>

<!-- ÕæʵmysqlÊý¾Ý¿âIP -->
<property name="ipAddress">192.168.116.132</property>
<property name="schema">aa,bb</property>

<!-- ÓÃÓڵǽmysqlµÄÓû§Ãû -->
<property name="user">amoeba</property>

<!-- ÓÃÓڵǽmysqlµÄÃÜÂë -->

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

</factoryConfig>

<!-- ObjectPoolʵÏÖÀà -->
<poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
<property name="maxActive">200</property>
<property name="maxIdle">200</property>
<property name="minIdle">10</property>
<property name="minEvictableIdleTimeMillis">600000</property>
<property name="timeBetweenEvictionRunsMillis">600000</property>
<property name="testOnBorrow">true</property>
<property name="testWhileIdle">true</property>
</poolConfig>
</dbServer>


<dbServer name="slave" virtual="true">
<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
<!-- ¸ºÔؾùºâ²ÎÊý 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
<property name="loadbalance">1</property>

<!-- ²ÎÓë¸Ãpool¸ºÔؾùºâµÄpoolNameÁбíÒÔ¶ººÅ·Ö¸î -->
<property name="poolNames">server2</property>
</poolConfig>
</dbServer>
</dbServerList>

<queryRouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter">
<property name="ruleConfig">${amoeba.home}/conf/rule.xml</property>
<property name="functionConfig">${amoeba.home}/conf/functionMap.xml</property>
<property name="ruleFunctionConfig">${amoeba.home}/conf/ruleFunctionMap.xml</property>
<property name="LRUMapSize">1500</property>
<property name="defaultPool">master</property>

<property name="writePool">master</property>
<property name="readPool">slave</property>
<property name="needParse">true</property>
</queryRouter>
</amoeba:configuration>
######amoeba主配置信息

vi /usr/local/amoeba/bin/amoeba
DEFAULT_OPTS="-server -Xms256m -Xmx256m -Xss256k" ###128k改成256k

###启动amoeba
nohup bash -x /usr/local/amoeba/bin/amoeba &
##测试服务是否启动
netstat -an | grep 9000

yum -y install mysql
mysql -uroot -p -h 192.168.116.129 -P 9000 ##本机amoeba密码:456

###测试amoeba效果,关闭从,测试主是否可读可写

posted @ 2018-05-24 15:41  疯狂的米粒儿  阅读(175)  评论(0编辑  收藏  举报