安装包:官网下载安装程序 http://downloads.mysql.com/archives/community/
mysql-community-client-5.7.9-1.el6.x86_64.rpm
mysql-community-common-5.7.9-1.el6.x86_64.rpm
mysql-community-libs-5.7.9-1.el6.x86_64 (1).rpm
mysql-community-server-5.7.9-1.el6.x86_64.rpm
yum本地安装
yum localinstall mysql-community*
初始化数据库,修改密码
mysqld_safe --skip-grant-tables
update mysql.user set authentication_string=password('mysql') where user="root";
flush privileges;
alter user 'root'@'localhost' identified by 'root';
增加一个用于同步数据的账户并设置相关的权限
set global validate_password_policy=0; --设置密码策略
grant replication slave on *.* to 'rsync'@'192.168.%.%' identified by 'rsync@2016'; ##增加同步账号,并附加权限
修改mysql配置文件my.cnf
/etc/my.cnf
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| xmj |
+--------------------+
5 rows in set (0.06 sec)
创建数据表测试
mysql> use xmj;
Database changed
mysql> create table xmj(id int(3),name char(10));
Query OK, 0 rows affected (0.09 sec)
mysql> show tables;
+---------------+
| Tables_in_xmj |
+---------------+
| xmj |
+---------------+
1 row in set (0.00 sec)
在表xmj中插入一条记录
mysql> insert into xmj values (001,'yixiuge');
Query OK, 1 row affected (0.07 sec)
mysql> select * from xmj;
+------+---------+
| id | name |
+------+---------+
| 1 | yixiuge |
+------+---------+
1 row in set (0.00 sec)
查看salve数据库和表是否同步
mysql> use xmj
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+---------------+
| Tables_in_xmj |
+---------------+
| xmj |
+---------------+
1 row in set (0.00 sec)
mysql> show tables;
+---------------+
| Tables_in_xmj |
+---------------+
| xmj |
+---------------+
1 row in set (0.00 sec)
mysql> select * from xmj;
+------+---------+
| id | name |
+------+---------+
| 1 | yixiuge |
+------+---------+
1 row in set (0.00 sec)
测试数据库和表都正常同步,此刻主从复制架构已经搭建完成
但是目前我们salve数据库还具有可写入操作,这样容易造成主从同步的时候数据库错乱和损坏,
此时就需要设置,slave为只读模式
查看数据库的运行状态
mysql> show global variables like 'read%';
+----------------------+--------+
| Variable_name | Value |
+----------------------+--------+
| read_buffer_size | 131072 |
| read_only | OFF |
| read_rnd_buffer_size | 262144 |
+----------------------+--------+
3 rows in set (0.15 sec)
此时read_only = off 只读模式处于关闭状态,我们打开
mysql> set global read_only=1;
Query OK, 0 rows affected (0.07 sec)
mysql> show global variables like 'read%';
+----------------------+--------+
| Variable_name | Value |
+----------------------+--------+
| read_buffer_size | 131072 |
| read_only | ON |
| read_rnd_buffer_size | 262144 |
+----------------------+--------+
3 rows in set (0.00 sec)
#这里是直接修改的,但是重启后会还原,还需要加入配置文件中
vi /etc/my.cnf
添加一行
read-only = on