欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

架设java mysql主备及HA切换服务器

程序员文章站 2022-07-14 14:25:34
...

目的: 用两台PC server实现主从备份,其中OS和应用的热备由HA完成,数据库采用mysql,采用主从模式实现数据库热备。

两台服务器是hp dl380g5,硬盘采用预设运行的raid5。

  •  
    • raid5的定义

RAID5需要三块或三块以上同厂家、同型号、同容量的硬盘搭建,硬盘需要支持热插拔,所组成的阵列容量是所有硬盘容量减去少于一块硬盘的容量之差 。在三盘数据存储之外,还会在存储的同时自动生成奇、偶校验信息,分别存储在不同的硬盘里,占据相对微小的空间。奇偶校验信息耗费的空间有限,但恢复数据的能力却庞大无比。拔出故障盘,换上无故障的新盘,存储于另外一盘中的相对奇或偶校验,均能依据存储在不同盘中的奇、偶校验信息对数据进行有效的恢复。

接着是分区,结果如下:

/boot 2g
/home 40g
/ 30g
/var 30g
/usr 30g
/usr/local 10g
/opt 60g
swap 8g
 

  安装jdk和设置环境变量

  • 下载jdk-1_5_0_15-linux-amd64.bin
  • 放到/usr/local下面
  • 解压
  • 将下列脚本放入.bash_profile中
    PATH=$PATH:$HOME/bin:/usr/local/jdk1.5.0_15/bin
    CLASSPATH=/usr/local/jdk1.5.0_15/lib:/usr/local/jdk1.5.0_15/jre/lib
    JAVA_HOME=/usr/local/jdk1.5.0_15
    export PATH
    export CLASSPATH
    export JAVA_HOME
      
  • 执行 su - 使环境变量生效
  • 可以卸载自带的jdk
    卸载自带的jdk
    用root用户登陆到系统,打开一个终端输入
    # rpm -qa|grep gcj
    显示内容其中包含下面两行信息
    libgcj-4.1.1-52.el5
    libgcj-4.1.1-52.el5
    java-1.4.2-gcj-compat-1.4.2.0-40jpp.112

    卸载 java-1.4.2-gcj-compat-1.4.2.0-40jpp.112
    # rpm -e java-1.4.2-gcj-compat-1.4.2.0-40jpp.112
     
  • 验证一下 java -version

安装mysql

  • 下载src包
写道
shell> groupadd mysql
shell> useradd -g mysql mysql
shell> gunzip < mysql-VERSION.tar.gz | tar -xvf -
shell> cd mysql-VERSION
shell> ./configure --prefix=/opt/mysql --with-charset=utf8
shell> make
shell> make install
shell> cp support-files/my-huge.cnf /etc/my.cnf
shell> cp support-files/mysql.server /etc/init.d/mysqld ; chmod
shell> ln -s ../init.d/mysql S85mysqld && ln -s ../init.d/mysql K85mysqld
shell> cd PREFIX
shell> bin/mysql_install_db --user=mysql
shell> chown -R root .
shell> chown -R mysql var
shell> bin/mysqld_safe --user=mysql &
shell>/opt/mysql/share/mysql/mysql.server stop #停止

开放远程访问权限
* 登录mysql后, grant all on *.* to 'mysql'@'ip' identified by 'password';
在my.cnf中设置默认引擎是innodb
default-storage-engine=INNODB

 

写道
shell> mysql -h host -u user -p
shell> mysql -h host -u user -p menagerie
shell> mysql < batch-file > mysql.out

mysql> SELECT VERSION(), CURRENT_DATE;
mysql> \c #表示取消
mysql> SHOW DATABASES;
mysql> USE test
mysql> CREATE DATABASE menagerie; #database名和table名大小写敏感,建议只采用小写
mysql> CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20),
    -> species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);
mysql> DESCRIBE table;
mysql> LOAD DATA LOCAL INFILE '/path/pet.txt' INTO TABLE pet LINES TERMINATED BY '\r\n';
mysql> INSERT INTO pet
    -> VALUES ('Puffball','Diane','hamster','f','1999-03-30',NULL);
mysql> SELECT * FROM pet WHERE name REGEXP '^b'; 大小写不敏感
mysql> SELECT * FROM pet WHERE name REGEXP BINARY '^b'; 大小写敏感
mysql> source filename ; #在mysql中执行sql语句脚本文件
mysql> \. filename
CREATE TABLE animals (
     id MEDIUMINT NOT NULL AUTO_INCREMENT ,
     name CHAR(30) NOT NULL,
     PRIMARY KEY (id)
 ); #The AUTO_INCREMENT attribute can be used to generate a unique identity for new rows

select中的一些函数:
(RIGHT(CURDATE(),5)<RIGHT(birth,5)) 截取字符串
YEAR(CURDATE()) 获取年份
MAX(article)
CONCAT(p1.id, p1.tvab) + 0 AS tvid



 

shell> mysqldump [options] db_name [tables]
shell> mysqldump [options] --databases db_name1 [db_name2 db_name3...]
shell> mysqldump [options] --all-databases

shell> mysqlimport [options] db_name textfile1 [textfile2 ...]

mysql> show engines\G

shell> chown -R user_name /path/to/mysql/datadir

 

配置Master-Master的同步

1. 两台服务器做ttt,分别叫做ttt1和ttt2

2. 启动或关闭mysql的脚本

/opt/mysql/share/mysql/mysql.server {start|stop}

3. 给ttt1和ttt2的mysql互相Grant权限

4. master-master的配置关键在于设置auto_increment_increment 和 auto_increment_offset

这两个东西其实影响的是mysql里面自增字段的增长方式。比如某个走1 3 5, 另一个就会走2 4 6.

一般配置的原则是:

      如果有N 个Mysql 几点(N=2),那么auto_increment_increment就在所有的节点上设置成N,同时每个节点

      设置一个不同的auto_increment_offset值(1, 2, ..., N)

ttt1上

log-bin=mysql-bin
server-id       = 1
replicate-same-server-id = 0
auto-increment-increment = 2
auto-increment-offset = 1

master-host=ttt2
master-port=3306
master-user=mysql
master-password=mysql
master-connect-retry=60

relay-log = slave-relay.log
relay-log-index = slave-relay-log.index
expire_logs_days        = 10
max_binlog_size         = 2000M
slave-skip-errors=126,1062
 

 

ttt2上

log-bin=mysql-bin
server-id       =2
replicate-same-server-id = 0
auto-increment-increment = 2
auto-increment-offset = 2

master-host=ttt1
master-port=3306
master-user=mysql
master-password=mysql
master-connect-retry=60
log-bin=mysql-bin
relay-log = slave-relay.log
relay-log-index = slave-relay-log.index
expire_logs_days        = 10
max_binlog_size         = 2000M
slave-skip-errors=126,1062

 5将ttt1上的数据库dump到ttt2上

ttt1>
mysqldump -u root --opt exampledb > snapshot.sql
scp snapshot.sql root@ttt2:/tmp

ttt2>
/usr/bin/mysqladmin --user=root --password=yourrootsqlpassword stop-slave
cd /tmp
mysql -u root  exampledb < snapshot.sql

 6. 查看各自master的状态

    show master status

 7. 指定各自master开始读的位置

CHANGE MASTER TO MASTER_HOST='ttt1', MASTER_USER='mysql', MASTER_PASSWORD='mysql', MASTER_LOG_FILE='mysql-bin.000009', MASTER_LOG_POS=84828;

CHANGE MASTER TO MASTER_HOST='ttt2', MASTER_USER='mysql', MASTER_PASSWORD='mysql', MASTER_LOG_FILE='mysql-bin.000005', MASTER_LOG_POS=84828;

 8. 查看并验证

    在ttt1和ttt2上输入show slave status。输出结果中

           Slave_IO_Running: Yes
          Slave_SQL_Running: Yes

   需要是yes。

FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS;
SHOW SLAVE STATUS\G
exit;
mysql> UNLOCK TABLES;

mysql> CHANGE MASTER;
mysql> stop slave;
mysql> start slave;
mysql> reset master;

 

http://www.linuxdiyf.com/bbs/viewthread.php?tid=64101&highlight=

 

番外--mysql的rails驱动安装

http://www.tmtm.org/en/mysql/ruby/
tar xzvf mysql-ruby-2.7.3.tar.gz
cd mysql-ruby-2.7.3
ruby extconf.rb --with-mysql-dir=/opt/mysql5
make && make install