shell> groupadd mysql
shell> useradd -g mysql mysql
shell> gunzip < mysql-VERSION.tar.gz | tar -xvf -
shell> cd mysql-VERSION
shell> ./configure --prefix=/usr/local/mysql
shell> make
shell> make install
shell> cp support-files/my-medium.cnf /etc/my.cnf
shell> cd /usr/local/mysql
shell> bin/mysql_install_db --user=mysql
shell> chown -R root .
shell> chown -R mysql var
shell> chgrp -R mysql .
shell> bin/mysqld_safe --user=mysql &
MySQL 主从同步
先清楚两点 1、mysql配置文件my.cnf的位置
2、如何启动、停止mysql,找好启动文件
假设有两台机器,已经安装好了mysql(尽量同版本,且两台机器同一网络,可以ping通)
主机A: 192.168.1.100
从机B:192.168.1.101 可以有多台从机
1、先登录主机 A
mysql>GRANT REPLICATION SLAVE ON .TO backup@"%" IDENTIFIED BY '123456';
赋予从机权限,有多台丛机,就执行多次
2、 打开主机A的my.cnf,输入
server-id = 1 #主机标示,整数
log_bin = /var/log/mysql/mysql-bin.log #确保此文件可写
read-only =0 #主机,读写都可以
binlog-do-db =test #需要备份数据,多个写多行
binlog-ignore-db=mysql #不需要备份的数据库,多个写多行
3、打开从机B的my.cnf,输入
server-id = 2
log_bin = /var/log/mysql/mysql-bin.log
master-host =192.168.1.100
master-user =backup
master-pass =123456
master-port =3306
master-connect-retry=60 #如果从服务器发现主服务器断掉,重新连接的时间差(秒)
replicate-do-db =test #只复制某个库
replicate-ignore-db=mysql #不复制某个库
4.重启slave mysql .
stop slave;
CHANGE MASTER TO MASTER_HOST=192.168.100.13, MASTER_USER=slave, MASTER_PASSWORD=slave_password, MASTER_LOG_FILE=mysql-bin.000001, MASTER_LOG_POS=890;
start slave;
5.在master mysql上创建数据库测试,从库是否同步.或者show slave statusG 查看
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
两个yes为同步.
可以在主机A中,做一些INSERT, UPDATE, DELETE 操作,看看主机B中,是否已经被修改
补充:
在从服务器上使用show slave statusG
Slave_IO_Running,为No,
则说明IO_THREAD没有启动,请执行start slave io_thread
Slave_SQL_Running为No
则复制出错,查看Last_error字段排除错误后执行start slave sql_thread
查看Slave_IO_State字段空 //复制没有启动
Connecting to master//没有连接上master
Waiting for master to send event//已经连上
主服务器上的相关命令:
show master status
show slave hosts
show logs
show binlog events
purge logs to log_name
purge logs before date
reset master(老版本flush master)
set sql_log_bin=
从服务器上的相关命令:
slave start
slave stop
SLAVE STOP IO_THREAD //此线程把master段的日志写到本地
SLAVE start IO_THREAD
SLAVE STOP SQL_THREAD //此线程把写到本地的日志应用于数据库
SLAVE start SQL_THREAD
reset slave
SET GLOBAL SQL_SLAVE_SKIP_COUNTER
load data from master
show slave status(SUPER,REPLICATION CLIENT)
CHANGE MASTER TO MASTER_HOST=, MASTER_PORT=,MASTER_USER=, MASTER_PASSWORD= //动态改变master信息
PURGE MASTER [before date] 删除master端已同步过的日志