您的位置:首页 > 数据库 > MySQL

mysql5.5.30源码安装及主从搭建

2015-02-04 10:43 429 查看
双机热备(实验环境)

主服务器:ip地址192.168.100.244,mysql版本5.5.30,源码安装
从服务器:ip地址192.168.100.245

一、源码安装mysql5.5

启动目录:/usr/local/mysql
数据文件目录:/data/mysql
二进制日志目录:/data/mysql/binlog
1.添加mysql 用户(不添加用户目录)

shell> groupadd mysql
shell> useradd -r -g mysql mysql

-g, --gid GROUP
The group name or number of the user’s initial login group. The group name must exist. A group number must refer to an already existing group. /etc/default/useradd.

-r This flag is used to create a system account. That is, a user with a UID lower than the value of UID_MIN defined in /etc/login.defs and whose password does not expire. Note that useradd will not create a home directory for such an user, regardless of the default setting in /etc/login.defs. You have to specify -m option if you want a home directory for a system account to be created. This is an option added by Red Hat
ps:

# yum install gcc-c++
# yum install cmake`
# yum install -y ncurses-devel


2.编译和安装

shell> cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DDEFAULT_CHARSET=latin1 \
-DDEFAULT_COLLATION=latin1_swedish_ci\
-DENABLED_LOCAL_INFILE=ON \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITHOUT_PARTITION_STORAGE_ENGINE=1 \
-DWITH_COMMENT="jss edition" \
-DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \
-DSYSCONFDIR=/data/mysql/
shell> make
shell> make test
shell> make install


-DCMAKE_INSTALL_PREFIX=/usr/local/mysql 指定启动目录存放路径

-DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock 指定sock存放路径
-DSYSCONFDIR=/data/mysql/ 指定数据库文件存放位置

3.部分机器需要安装cmake[cmake-2.8.12.2.tar.gz]

shell> yum install ncurses-devel
shell> yum install bison
shell > yum install gcc*
shell>  ./bootstrap --prefix=/usr/local/cmake
shell> make
shell> make install


4.修改数据目录权限

shell> chown -R mysql:mysql mysql/
5.导入数据库文件
cd /usr/local/mysql/
./scripts/mysql_install_db --user=mysql --datadir=/data/mysql/
6.添加mysql服务
cd /usr/local/mysql/support-files/
cp -rp mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig --level 345 mysqld on
7.修改配置文件

[client]
port            = 3306
socket          = /data/mysql/mysql.sock
default_character-set=utf8

[mysqld]
port            = 3306
socket          = /data/mysql/mysql.sock
character_set_server=utf8
log-error=/data/mysql/log/error.log
datadir=/data/mysql/data/
max_connections = 16384
table_cache = 64

slow_query_log_file=/data/mysql/mysql-slow.log
long_query_time=3

symbolic-links=0

skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
expire_logs_days = 1
log-bin=/data/mysql/binlog/mysql-bin
binlog_format=STATEMENT
server-id       = 2
innodb_buffer_pool_size = 16M
innodb_additional_mem_pool_size = 2M
innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
innodb_file_per_table=1
innodb_flush_method = O_DIRECT
innodb_file_io_threads = 4

innodb_data_home_dir=/data/mysql/data
innodb_data_file_path=ibdata1:1G:autoextend


8.启动mysqld服务

/etc/init.d/mysqld start
9.添加/usr/local/mysql/bin 到PATH
在/etc/profile最后一行添加:export $PATH="$PATH:/usr/local/mysql/bin"
保存退出,执行shell> source /etc/profile
10.设置mysql密码
mysqladmin -u root password "newpassword"

二、主从配置

1.主服务器bin-log配置
log-bin=/data/mysql/binlog/mysql-bin
expire_logs_days=1
可选配置:

binlog-do-db=fire
binlog-ignore-db=mysql #这个最好不要添加,因为存储过程都是存在msyql库的proc表里

2.主库新建从库账户

grant replication slave on *.* to 'slave-user1'@'192.168.1.72' identified by 'redhat';
flush privileges;
3.关闭主服务器mysql表写入,dump数据库
3.1先查询当前正在执行的操作是否结束:show processlist;
3.2 在所有操作执行完后,
flush tables with read lock;
读master进程状态:mysql > show master status \G
此时,lock表的终端不要关闭,因为关闭会导致数据库解锁。
开启另外一个终端,dump数据库:# mysqldump -uroot -p --opt -R -B -F --databases fire | gzip > db.tar.gz
--opt --add-drop-table + --add-locks + --create-options + --disables-keys + --extended-insert + --lock-tables + --quick + --set+charset
-R 在转储的数据库中转储存储程序(函数和程序)。使用---routines产生的输出包含CREATE PROCEDURE和CREATE FUNCTION语句以重新创建子程序。但是,这些语句不包括属性,例如子程序定义者或创建和修改时间戳。这说明当重载子程序时,对它们进行创建时定义者应设置为重载用户,时间戳等于重载时间。
如果你需要创建的子程序使用原来的定义者和时间戳属性,不使用--routines。相反,使用一个具有mysql数据库相应权限的MySQL账户直接转储和重载mysql.proc表的内容。

该选项在MySQL 5.1.2中添加进来。在此之前,存储程序不转储。

-B 转储几个数据库。通常情况,mysqldump将命令行中的第1个名字参量看作数据库名,后面的名看作表名。使用该选项,它将所有名字参量看作数据库名。CREATE DATABASE IF NOT EXISTS db_name和USE db_name语句包含在每个新数据库前的输出中。
-F 开始转储前刷新MySQL服务器日志文件。该选项要求RELOAD权限。请注意如果结合--all--database(或-A)选项使用该选项,根据每个转储的数据库刷新日志。例外情况是当使用--lock-all-tables或--master-data的时候:在这种情况下,日志只刷新一次,在所有 表被锁定后刷新。如果你想要同时转储和刷新日志,应使用--flush-logs连同--lock-all-tables或--master-data。
对主库解锁:unlock tables;
打包 # tar -zcf fire.tar.gz ./fire.sql
复制到从服务器
4.配置从服务器master、开启slave进程

mysql> change master to master_host='192.168.100.252', master_user='slave-user1', master_password='1234',
master_log_file='mysql-bin.000001', master_log_pos=479;

解压主服务器的fire库 # tar -xvf fire.tar.gz
在主服务器添加fire数据库 mysql > create database fire ;
附加数据到fire库 # mysql -uroot -p fire < fire.sql
开slave进程 mysql > start slave ;
mysql > show slave status ;
5.开启主服务器mysql表写入,查看从服务器的slave进程状态
mysql > show master status ;

三、后期维护

1.二进制日志过多的时候,用sql语句删除过多的日志

mysql> purge binary logs to 'mysql-bin.000128';
Query OK, 0 rows affected (2.92 sec)

将二进制日志删除到000128
直接进数据库查看purge_binary_logs的属性值,通过set purge_binary_logs=2来设置
2.主从服务器服务重启顺序
3.mysqldump对数据库版本的要求
一鈡情况会出现mysqldump 数据报错
mysqldump: Couldn't execute 'SET OPTION SQL_QUOTE_SHOW_CREATE=1': You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OPTION SQL_QUOTE_SHOW_CREATE=1' at line 1 (1064)
参考网上较多的解决方案:
1)使用与mysql server相同版本的mysqldump的绝对路径进行dump,如:/usr/bin/mysqldump -uroot -p databasename > sql.sql
2)核对数据库版本(server版本和client版本),低client版本不能导出高server版本的数据库(如5.5的mysqldump不能导出5.6的server的数据)
4. 从库配置好master后,启动slave,报错:

Slave_IO_Running: Yes
Slave_SQL_Running: No

Last_Error: Error 'Duplicate entry '11531105-356' for key 1' on query. Default database: 'db_bayu_5'. Query: 'INSERT INTO `t_quest`SET`rid`=11531105,`quest_id`=356,`type`=0,`status`=1,`target_idx`=0,`quest_loop`=0,`progress`='',`entrust`=2,`star`=0,`money_type`=0,`left_count`=1,`last_round_date`=1405642337'
分析:配置的从库和mysqldump的时间不协调,出现了很多redo错误
5. error.log

140827 10:04:41 InnoDB: ERROR: the age of the last checkpoint is 9433834,
InnoDB: which exceeds the log group capacity 9433498.
InnoDB: If you are using big BLOB or TEXT rows, you must set the
InnoDB: combined size of log files at least 10 times bigger than the
InnoDB: largest such row.

At first glance, I would say that your innodb_log_file_size is way too small. It should be bigger to do two things:

Accommodate any big BLOB or TEXT fields

Holding bigger transactions

Here is what you should do for now to see if it helps:

STEP 01) Change the following in /etc/my.cnf

[mysqld]innodb_log_buffer_size          = 32M

innodb_buffer_pool_size = 3G
innodb_log_file_size           = 768M

STEP 02)
service mysql stop


STEP 03)
rm -f /var/lib/mysql/ib_logfile*


STEP 04)
service mysql start


This will rebuild the following files

/var/lib/mysql/ib_logfile0

/var/lib/mysql/ib_logfile1

Give it a Try !!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: