时间:2021-07-01 10:21:17 帮助过:17人阅读
MySQL是目前轻量级数据库MySQL的冗余方案之首选 以下是MySQL的异步主从复制的配置过程 先配置系统yum,此处不已源码包的安装过
MySQL是目前轻量级数据库MySQL的冗余方案之首选
以下是MySQL的异步主从复制的配置过程
先配置系统yum,此处不已源码包的安装过程为例来介绍了!
也不再介绍yum的配置过程,从安装mysql开始
推荐阅读:
此处用两个主机安装
master:192.168.0.111
slave:192.168.0.222
安装MySQL(主从两个节点上都安装)
yum -y install mysql mysql-server
创建mysql的数据目录(两个节点)
mkdir /opt/mysql_data && chown -R mysql.mysql /opt/mysql_data && chmod 766 /opt/mysql_data
配置mysqld(master节点)
编辑/etc/my.cnf内容如下,先启动mysql的(两个节点):
[mysqld]
character-set-server = utf8
datadir=/opt/mysql_data
socket=/tmp/mysql.sock
user=mysql
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
启动mysqld:
service mysqld start && chkconfig --level 35 mysqld on
************************************************
注意:此处以主从复制为主题不讨论iptables
和SELinux,,所以事先关闭SELinux和iptables
否则mysqld会起不来
************************************************
创建要同步的数据库和同步的用户(在master节点)
mysql> grant replication slave,reload,super on *.* to cai@192.168.0.222 identified by 'cai'; //创建cai用户
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on *.* to cai@192.168.0.222 identified by 'cai';
Query OK, 0 rows affected (0.00 sec)
mysql> create database caidb; //创建要同步的数据库
Query OK, 1 row affected (0.00 sec)
mysql> show databases; select user,host from mysql.user; //查看创建的库和用户信息
+--------------------+
| Database |
+--------------------+
| information_schema |
| caidb |
| mysql |
| test |
+--------------------+
4 rows in set (0.01 sec)
+------+---------------+
| user | host |
+------+---------------+
| root | 127.0.0.1 |
| cai | 192.168.0.222 |
| | localhost |
| root | localhost |
| | master |
| root | master |
+------+---------------+
6 rows in set (0.00 sec)
在slave节点上尝试连接master,测试网络和用户的可用性:
[root@slave ~]# mysql -ucai -pcai -h 192.168.0.111 -e "select user,host from mysql.user;show databases;" //用cai登录远程数据库192.168.0.111,执行sql “show databases”
+------+---------------+
| user | host |
+------+---------------+
| root | 127.0.0.1 |
| cai | 192.168.0.222 |
| | localhost |
| root | localhost |
| | master |
| root | master |
+------+---------------+
+--------------------+
| Database |
+--------------------+
| information_schema |
| caidb |
| mysql |
| test |
+--------------------+
在要同步的数据库中创建表和模拟数据(在master)
mysql> create table `caitable` (`id` int(5) NOT NULL auto_increment, `name` varchar(32) NOT NULL, `password` char(32) NOT NULL, PRIMARY KEY (`id`)); //创建表
插入模拟数据用一下脚本:
[root@master ~]# cat ist.sh
#!/bin/bash
for ((i=1; i<=10; i++)) //循环插入10条数据
do
mysql -uroot -e "insert into caidb.caitable(id,name,password) value ('$i','user$i',md5('usercai$i'));"
done
mysql -uroot -e "select * from caidb.caitable;" //查询插入的数据
接下来请看第2页: