linux MySql 主从异步复制

[root@localhost ~]# hostname master.allentuns

###SLAVE 执行

[root@localhost ~]# sed -i 's@(HOSTNAME=).*@1slave.allentuns.com@g' /etc/sysconfi g/network

[root@localhost ~]# hostname slave.allentuns

 配置主机名解析

修改 hosts 文件来实现

###MASTER 执行

[root@master ~]# cat >> /etc/hosts << EOF

> 192.168.2.100 master.allentuns.com

> 192.168.2.200 slave.allentuns.com

> EOF

###SLAVE 执行

[root@slave ~]# cat >> /etc/hosts << EOF

> 192.168.2.100 master.allentuns.com

> 192.168.2.200 slave.allentuns.com

> EOF

同步服务器时间

###MASTER 执行

[root@master ~]# crontab -l

*/5 * * * * /usr/sbin/ntpdate -u asia.pool.ntp.org

###SLAVE 执行

*/5 * * * * /usr/sbin/ntpdate -u asia.pool.ntp.org

4.  安装 MySql

4.1 在 Master 与 Slave 服务器上分别安装 Mysql

###MASTER执行

1、解压 mysql 并建立软链接

tar xf mysql-5.6.12-linux-glibc2.5-x86_64.tar.gz -C /usr/local/

cd /usr/local/

ln -sv mysql-5.6.12-linux-glibc2.5-x86_64 mysql

2、创建 mysql 用户

groupadd -r mysql

useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql

3、赋予 mysql 程序目录的属主和属组为 mysql 用户

chown -R mysql.mysql /usr/local/mysql/

4、创建 mysql 数据存储目录并赋予此目录的属主和属组为 mysql 用户

mkdir -pv /mydata/data

chown -R mysql:mysql /mydata/data/

5、初始化 mysql 依赖 libaio yum -y install libaio

6、初始化 mysql

/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/mydata/d ata --user=mysql

7、为了 mysql 数据库的安全,改回 mysql 程序目录的属主

chown -R root /usr/local/mysql/*

8、修改 mysql 的默认配置文件,并优化相关参数

# vim /usr/local/mysql/my.cnf

添加内容是:

log-bin = master-bin

server-id = 1 port = 3306

basedir = /usr/local/mysql/

datadir = /mydata/data

socket = /var/lib/mysql/mysql.sock innodb_file_per_table = 1

9、为 mysql 提供 sysv 服务脚本并启动服务

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

10、将 mysql 加入到服务列表并设置开机自启动

chkconfig --add mysqld chkconfig mysqld on

11、mysql 命令添加到环境变量中

# vim /etc/profile.d/mysql.sh

export PATH=$PATH:/usr/local/mysql/bin

# source /etc/profile

12、输出 mysql 的 man 手册到 man 命令的查找路径

# yum -y install man

# vim /etc/man.config +54 加入到 54 行后

MANPATH /usr/local/mysql/man

13、输出 mysql 的库文件

echo "/usr/local/mysql/lib/" > /etc/ld.so.conf.d/mysql.conf ldconfig -v

14、输出 mysql 的头文件到系统头文件

ln -sv /usr/local/mysql/include/ /usr/include/mysql

service mysqld start

16、测试 mysql[root@mysql ~]# mysql

Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 1

Server version: 5.6.12-log MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql> show databases;

+--------------------+

| Database          |

+--------------------+

| information_schema |

| mysql             |

| performance_schema |

| test              |

+--------------------+

4 rows in set (0.00 sec)

mysql> q   Bye

###SLAVE执行

1、解压 mysql 并建立软链接

tar xf mysql-5.6.12-linux-glibc2.5-x86_64.tar.gz -C /usr/local/

cd /usr/local/

ln -sv mysql-5.6.12-linux-glibc2.5-x86_64 mysql

2、创建 mysql 用户

groupadd -r mysql

useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql

3、赋予 mysql 程序目录的属主和属组为 mysql 用户

chown -R mysql.mysql /usr/local/mysql/

4、创建 mysql 数据存储目录并赋予此目录的属主和属组为 mysql 用户

mkdir -pv /mydata/data

chown -R mysql:mysql /mydata/data/

5、初始化 mysql 依赖 libaio yum -y install libaio

/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/mydata/d

ata --user=mysql

7、为了 mysql 数据库的安全,改回 mysql 程序目录的属主

chown -R root /usr/local/mysql/*

8、修改 mysql 的默认配置文件,并优化相关参数

# vim /usr/local/mysql/my.cnf

添加内容是: [mysqld]

log-bin = master-bin server-id = 1

port = 3306

basedir = /usr/local/mysql/

datadir = /mydata/data

socket = /var/lib/mysql/mysql.sock innodb_file_per_table = 1

9、为 mysql 提供 sysv 服务脚本并启动服务

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

10、将 mysql 加入到服务列表并设置开机自启动

chkconfig --add mysqld chkconfig mysqld on

11、mysql 命令添加到环境变量中

# vim /etc/profile.d/mysql.sh

export PATH=$PATH:/usr/local/mysql/bin

# source /etc/profile

12、输出 mysql 的 man 手册到 man 命令的查找路径

# yum -y install man

# vim /etc/man.config +54 加入到 54 行后

MANPATH /usr/local/mysql/man

13、输出 mysql 的库文件

echo "/usr/local/mysql/lib/" > /etc/ld.so.conf.d/mysql.conf ldconfig -v

14、输出 mysql 的头文件到系统头文件

ln -sv /usr/local/mysql/include/ /usr/include/mysql

15、启动 mysql 服务

service mysqld start

16、测试 mysql[root@mysql ~]# mysql

Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 1

Server version: 5.6.12-log MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql> show databases;

+--------------------+

| Database          |

+--------------------+

| information_schema |

| mysql             |

| performance_schema |

| test              |

+--------------------+

4 rows in set (0.00 sec)

mysql> q

Bye

原文地址:https://www.cnblogs.com/fanweisheng/p/11328135.html