mysql 主从复制 主主配置

来源:互联网 发布:koala mac版下载 编辑:程序博客网 时间:2024/06/09 11:59

服务器二台

  1.安装mysql 命令

          yum -y install mysql-server

  2.配置登录用户的密码

    启动mysql 服务

    

   设置密码并登录

    

    查看mysql用户

    

    为了数据库安全,把没有密码的msql用户删除

   

   现在只能localhost 能访问,修改 host字段其它机器就可以访问了

   

   

3.配置完成后,试一下两台机器能不能互相访问

 mysql -h192.168.1.104 -uroot -p

 mysql -h192.168.1.103  -uroot -p




配置my.cnf  

  vim /etc/my.cnf #如果etc没有my.cnf拷贝cp /usr/share/mysql/my-medium.cnf /etc/my.cnf

  server-id=1   #设置服务器id,为1表示主服务器,注意:必须与主服务器的以及其它从服务器的不相同,检查原来的配置文件中已经有这一行,就不用再添加了。 

 log-bin=mysql-bin  #启动MySQ二进制日志系统,注意:检查原来的配置文件中已经有这一行,就不用再添加了。

 service mysqld  restart  #重启MySQL 出错 msyqld: 未被识别的服务 试着把 mysqld 改成mysql : service mysql  restart

 登录mysql检查 service_id是否正确

mysql> show variables like 'server_id'
    -> ;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 1     |
+---------------+-------+
1 row in set (0.00 sec)


查看192.168.1.103 master

mysql> show master status
    -> ;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 |      107 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

在192.168.1.104上 执行以下配置

change master to
master_host='192.168.1.103',
master_user='root',
master_password='root',
master_log_file='mysql-bin.000002',
master_log_pos=107;


mysql> change master to
    -> master_host='192.168.1.103',#主的ip
    -> master_user='root',
    -> master_password='root',
    -> master_log_file='mysql-bin.000002',
    -> master_log_pos=107;
Query OK, 0 rows affected (0.09 sec)



查看192.168.1.104 master

mysql> show master status
    -> ;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 |      107 |              |                  |
+------------------+----------+--------------+------------------+

在192.168.1.103上 执行以下配置

change master to
master_host='192.168.1.104',
master_user='root',
master_password='root',
master_log_file='mysql-bin.000004',
master_log_pos=107;


mysql> change master to
    -> master_host='192.168.1.104',
    -> master_user='root',
    -> master_password='root',
    -> master_log_file='mysql-bin.000004',
    -> master_log_pos=107;
Query OK, 0 rows affected (0.19 sec)

查看从服务器的状态

mysql>show slave status\G

完成以上配置主主配置就完成了









0 0
原创粉丝点击