萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mysql教程 >> mysql 5.5數據庫主從配置步驟詳解

mysql 5.5數據庫主從配置步驟詳解

本文章來給大家介紹一篇mysql備份的功能,這個就是常用的mysql 5.5數據庫主從配置了,這個算是熱備份吧,你有數據更新就會自動更新了,相對來說安全也比較高了,下面我來給大家介紹一下。

上次給大家介紹了mysql 5.1主從搭建配置教程,這次我們來實現mysql 5.5的主從復制,其實大體上配置是差不多的,只有點細微的差別.
   系統:centos 5.x
   需要的軟件包:mysql-5.5.33.tar.gz
   環境准備:
        服務器a:192.168.10.151 (主)
        服務器b:192.168.10.152 (從)
 
1.安裝前准備
wget http://mysql.llarian.net/Downloads/MySQL-5.5/mysql-5.5.33.tar.gz
yum -y install gcc  gcc-c++ libtool-libs autoconf freetype-devel gd libjpeg-devel
libpng-devel libxml2-devel ncurses-devel zlib-devel zip unzip curl-devel wget crontabs
 file bison cmake patch mlocate flex diffutils automake make kernel-devel cpp
readline-devel openssl-devel vim-minimal glibc-devel  glib2-devel
bzip2-devel e2fsprogs-devel libidn-devel  gettext-devel expat-devel
libcap-devel  libtool-ltdl-devel pam-devel pcre-devel libmcrypt-devel

2.在服務器a上安裝mysql
tar zxf mysql-5.5.33.tar.gz && cd mysql-5.5.33
/usr/sbin/groupadd mysql
/usr/sbin/useradd -g mysql mysql
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=complex -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1

make
make install

chmod +w /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql
cd support-files/
cp my-medium.cnf /etc/my.cnf
cp -f mysql.server /etc/rc.d/init.d/mysqld
mkdir /var/lib/mysql
/usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/var/lib/mysql --user=mysql
chmod +x /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
chkconfig  mysqld on
cat > /etc/ld.so.conf.d/mysql.conf<<EOF
/usr/local/mysql/lib/mysql
/usr/local/lib
EOF
ldconfig

如果你是在64位機器上的話,那你就執行下面這個命令:
ln -s /usr/local/mysql/lib/mysql /usr/lib64/mysql

如果你是在32位機器上的話,那就執行下面的命令:
ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql

然後再執行:
ln -s /usr/local/mysql/bin/mysql /usr/bin
ln -s /usr/local/mysql/bin/mysqladmin /usr/bin
ln -s /usr/local/mysql/bin/mysqldump /usr/bin
service mysqld start
mysqladmin -u root password 'password'

當然如果大家覺得編譯安裝mysql很麻煩的話,那麼可以去看看這篇文章安裝mysql5.5二進制包.

服務器b安裝mysql配置跟服務器a一樣,這裡就不說了.

3.配置主從
在服務器a上:
vi /etc/my.cnf
[mysqld]
log-bin=master-bin
log-bin-index=master-bin.index
server-id       = 1
innodb_file_per_table = 1
binlog_format=mixed

授權復制用戶:
mysql -u root -p
grant replication slave on *.* to 'dbmysql'@'%' identified by '123456';
flush privileges;

重啟mysql
service mysqld restart

在服務器b上:
vi /etc/my.cnf
[mysqld]
relay-log = relay-log
relay-log-index = relay-log.index
server-id       = 2
innodb_file_per_table = 1
binlog_format=mixed

然後重啟mysql
service mysqld restart

服務器b連接連接主服務器並復制
先在服務器a上查看master的狀態
mysql> show master status;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000001 |      107 |              |                  |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

然後在服務器b上進行連接
mysql> change master to master_host='192.168.10.151',master_user='dbmysql',maste
r_password='123456',master_log_file='master-bin.000001',master_log_pos=107;

查看一下slave狀態
mysql> show slave statusG
*************************** 1. row ***************************
               Slave_IO_State:
                  Master_Host: 192.168.10.151
                  Master_User: dbmysql
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 107
               Relay_Log_File: localhost-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: master-bin.000001
             Slave_IO_Running: No
            Slave_SQL_Running: No
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 107
              Relay_Log_Space: 107
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 0
1 row in set (0.00 sec)

啟動slave再查看
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

mysql> show slave statusG
*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: 192.168.10.151
                  Master_User: dbmysql
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 107
               Relay_Log_File: localhost-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: master-bin.000001
             Slave_IO_Running: Connecting
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 107
              Relay_Log_Space: 107
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 2003
                Last_IO_Error: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 86400
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 0
1 row in set (0.00 sec)

看到上面錯誤了沒,說明mysql主和從服務器的防火牆沒有開放3306端口,去服務器a和服務器b上把3306端口打開,再來查看mysql從的狀態
mysql> show slave statusG
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.10.151
                  Master_User: dbmysql
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 107
               Relay_Log_File: relay-log.000011
                Relay_Log_Pos: 254
        Relay_Master_Log_File: master-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 107
              Relay_Log_Space: 404
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
1 row in set (0.00 sec)

好了,可以看到Slave_IO_Running和Slave_SQL_Running都是yes了,下面也沒有error提示了.

4.驗證
在mysql主上創建個數據庫
mysql> create database emlog;
Query OK, 1 row affected (0.01 sec)

再在mysql從上查看是否有這個數據庫
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| emlog              |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.02 sec)

可以看到已經同步復制過來了,mysql主從搭建成功.

copyright © 萬盛學電腦網 all rights reserved