mysql 主从配置:

mysql 主从配置:

master: 192.168.189.140
slave: 192.168.189.141

系统:centos 5.4

防火墙处于关闭状态(要开启的话,需要就要主服务器开放3306端口)

1、在两台服务器上安装mysql

 tar zxvf mysql-5.1.48.tar.gz
 cd mysql-5.1.48
 ./configure --prefix=/usr/local/mysql --localstatedir=/var/lib/mysql --with-comment=Source --with-server-suffix=-enterprise-gpl --with-mysqld-user=mysql --without-debug --with-big-tables --with-charset=utf8 --with-collation=utf8_general_ci --with-extra-charsets=all --with-pthread --enable-static --enable-thread-safe-client --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static --enable-assembler --without-ndb-debug  --with-plugins=myisam,innobase
 make && make install
 useradd mysql
 cd /usr/local/mysql
 bin/mysql_install_db --user=mysql
 chown -R root:mysql .
 chown -R mysql /var/lib/mysql
 chgrp -R mysql .
 cp share/mysql/mysql.server /etc/init.d/mysqld
 chmod 755 /etc/init.d/mysqld
 chkconfig --add mysqld
 
ln -s /usr/local/mysql/bin/mysqld_safe /usr/bin/mysqld_safe
 ln -s /usr/local/mysql/share/mysql/mysql.server /usr/bin/mysqld
 ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
 ln -s /usr/local/mysql/bin/mysqldump /usr/bin/mysqldump
 ln -s /usr/local/mysql/bin/mysqladmin /usr/bin/mysqladmin
 ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
 ln -s /usr/local/mysql/include/mysql /usr/include/mysql
 echo "/usr/local/mysql/lib/mysql" >> /etc/ld.so.conf
 ldconfig
 
 
 chkconfig --level 35 mysqld on
  service mysqld restart
  mysql -p  进入下看是否安装成功

2、my.cnf的配置
  
   a、在主服务器(master)上,设置一个从数据库的账户,使用REPLICATION SLAVE赋予权限,如:

   mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave001'@'192.168.189.141' IDENTIFIED BY'123456';
  
   修改主数据库的配置文件my.cnf,开启BINLOG,并设置server-id的值,修改之后必须重启Mysql服务
   cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf
   mkdir /var/log/mysql
   chown -R mysql.mysql /var/log/mysql/
  
   vi /etc/my.cnf  
  
   添加以下参数:
   server-id=1
   log_bin=/var/log/mysql/mysql-bin.log
   binlog-do-db=test
   binlog-ignore-db=mysql
  
   servies mysqld restart 看是否会正常启动 , 没有问题继续 配置从服务器(slave)
   查看:
   mysql> show master status\G;
*************************** 1. row ***************************
            File: mysql-bin.000002
        Position: 369
    Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)


  
   b、slave服务器上配置my.cnf
   cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf
   mkdir /var/log/mysql
   chown -R mysql.mysql /var/log/mysql/
  
   my.cnf中添加以下参数
   server-id=2
   log_bin=/var/log/mysql/mysql-bin.log
   master-host=192.168.189.140
   master-user=slave001
   master-port=3306
   master-connect-retry=60
   replicate-do-db=test
   replicate-ignore-db=mysql
  
   service mysqld restart测试mysql是否正常启动
  
   在从服务器上,启动slave进程
   mysql> start slave;
   在从服务器进行show salve status验证
   mysql> mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.189.140
                  Master_User: slave001
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 369
               Relay_Log_File: localhost-relay-bin.000003
                Relay_Log_Pos: 514
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: test
          Replicate_Ignore_DB: mysql

  
   一切OK,接下来就是测试master和slave服务器是否会同步数据
  
   3、测试
      在master服务器上创建一张表
       CREATE TABLE tab1 (
                                  id   INT,
                                  val  VARCHAR(10),
                                  PRIMARY KEY (id)
                                );
    
                               
    查看slave服务器上是否有tab1表,结果是此表存在                           
    mysql> desc tab1;
    +-------+-------------+------+-----+---------+-------+
    | Field | Type        | Null | Key | Default | Extra |
    +-------+-------------+------+-----+---------+-------+
    | id    | int(11)     | NO   | PRI | 0       |       |
    | val   | varchar(10) | YES  |     | NULL    |       |
    +-------+-------------+------+-----+---------+-------+
    2 rows in set (0.00 sec)
   
 

 

 

 问题:

 

MySQL同步时,出现的ERROR 1201 (HY000)错误解决方法

在MySQL主从复制的时候,在slave服务器上,登陆MySQL,

使用 stop slave;

这时候系统提示:

mysql> stop slave;  

Query OK, 0 rows affected (0.00 sec)

但当执行start slave;的时候,就出现了如下错误:

mysql> start slave;

ERROR 1201 (HY000): Could not initialize master info structure; more error messages can be found in the MySQL error log
mysql>

解决办法:

1.进入MySQL数据库默认的数据库存储目录:

/usr/local/mysql/var

这个目录根据实际情况而定,使用不同的安装方式,或是编译指定目录不同,这个目录也不一定相同,但只要找到数据库的存储目录就好。

 

2.删除以下两个文件:

master.info

relay-log.info

 

3.登陆数据库后,执行:

start slave;

返回如下信息:

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql>

这样就说明问题已经解决了。

 

你可能感兴趣的:(mysql,数据库,职场,休闲,主从配置)