mysql主從配置

來源:互聯網
上載者:User

標籤:mysql主從

1.安裝、配置MySQL 

2.啟動mysql服務及同步主從伺服器的資料[[email protected] ~]# /etc/init.d/mysqld startStarting MySQL SUCCESS! [[email protected] ~]# /etc/init.d/mysqld startStarting MySQL SUCCESS! [[email protected] ~]# rsync -avLP [email protected]:/data/mysql/ /data/mysql/   //將主伺服器上的資料與從伺服器上的資料同步3.配置主(master)[[email protected] ~]# vi /etc/my.cnf   //修改或添加server-id=1log-bin=mysql-bin 同時也可以添加以下參數:binlog-do-db=db1,db2 #需要同步的庫binlog-ignore-db=db1,db2 #忽略不同步的庫 [[email protected] ~]# /etc/init.d/mysqld restart   //修改設定檔後重啟服務Shutting down MySQL.. SUCCESS! Starting MySQL.. SUCCESS! [[email protected] ~]# mysql -uroot   //原生mysql未設密碼mysql> grant replication slave on *.* to ‘repl‘@‘192.168.1.119‘ identified by ‘123456‘;  //repl是為slave端設定的訪問master端mysql資料的使用者,密碼為123456;192.168.1.119為slave的ip。Query OK, 0 rows affected (0.06 sec)mysql> flush privileges;   //重新整理庫,記憶體的資料寫入磁碟Query OK, 0 rows affected (0.00 sec)mysql> flush tables with read lock;   //鎖定資料庫,此時不允許更改任何資料Query OK, 0 rows affected (0.00 sec)mysql> show master status;+------------------+----------+--------------+------------------+| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+------------------+| mysql-bin.000032 |      106 |              | mysql            |+------------------+----------+--------------+------------------+1 row in set (0.00 sec)4.設定從(slave)[[email protected] ~]# vi /etc/my.cnfserver-id = 2 #這個數值不能和主一樣replicate-do-db=db1,db2 和 replicate-ignore-db=db1,db2 #意義同主的那兩個選擇性參數,若主上配置過了則從就不用配置[[email protected] ~]# /etc/init.d/mysqld restart   //重啟服務Shutting down MySQL. SUCCESS! Starting MySQL. SUCCESS! [[email protected] ~]# mysql -urootmysql> slave stop;Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> change master to master_host=‘192.168.1.111‘, master_port=3306, master_user=‘repl‘, master_password=‘123456‘, master_log_file=‘mysql-bin.000032‘, master_log_pos=106;//MASTER_HOST:主伺服器的IP。MASTER_USER:配置主伺服器時建立的使用者名稱。MASTER_PASSWORD:使用者密碼。MASTER_PORT:主伺服器mysql連接埠,若為預設連接埠可省略。master_log_file和master_log_pos要和主上一致。Query OK, 0 rows affected (0.05 sec)mysql> slave start;Query OK, 0 rows affected (0.01 sec)mysql> show slave status\G;*************************** 1. row ***************************               Slave_IO_State: Waiting for master to send event                  Master_Host: 192.168.1.111                  Master_User: repl                  Master_Port: 3306                Connect_Retry: 60              Master_Log_File: mysql-bin.000032          Read_Master_Log_Pos: 106               Relay_Log_File: localhost-relay-bin.000002                Relay_Log_Pos: 251        Relay_Master_Log_File: mysql-bin.000032             Slave_IO_Running: Yes   //yes表示配置成功            Slave_SQL_Running: Yes  //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: 106              Relay_Log_Space: 410              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: 0Master_SSL_Verify_Server_Cert: No                Last_IO_Errno: 0                Last_IO_Error:               Last_SQL_Errno: 0               Last_SQL_Error:1 row in set (0.00 sec)ERROR:No query specified5.測試主從:mysql> unlock tables;  //主上資料庫已鎖,此為解鎖Query OK, 0 rows affected (0.00 sec)mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || aming              || mysql              || test               |+--------------------+4 rows in set (0.05 sec)mysql> create database cangls;Query OK, 1 row affected (0.00 sec)mysql> use cangls;Database changed mysql> select database();+------------+| database() |+------------+| cangls     |+------------+1 row in set (0.00 sec)mysql> show tables;Empty set (0.00 sec)mysql> CREATE TABLE `pre_forum_post` (    ->   `pid` int(10) unsigned NOT NULL,    ->   `fid` mediumint(8) unsigned NOT NULL DEFAULT ‘0‘,    ->   `tid` mediumint(8) unsigned NOT NULL DEFAULT ‘0‘,    ->   `first` tinyint(1) NOT NULL DEFAULT ‘0‘,    ->   `author` varchar(15) NOT NULL DEFAULT ‘‘,    ->   `authorid` mediumint(8) unsigned NOT NULL DEFAULT ‘0‘,    ->   `subject` varchar(80) NOT NULL DEFAULT ‘‘,    ->   `dateline` int(10) unsigned NOT NULL DEFAULT ‘0‘,    ->   `message` mediumtext NOT NULL,    ->   `useip` varchar(15) NOT NULL DEFAULT ‘‘,    ->   `port` smallint(6) unsigned NOT NULL DEFAULT ‘0‘,    ->   `invisible` tinyint(1) NOT NULL DEFAULT ‘0‘,    ->   `anonymous` tinyint(1) NOT NULL DEFAULT ‘0‘,    ->   `usesig` tinyint(1) NOT NULL DEFAULT ‘0‘,    ->   `htmlon` tinyint(1) NOT NULL DEFAULT ‘0‘,    ->   `bbcodeoff` tinyint(1) NOT NULL DEFAULT ‘0‘,    ->   `smileyoff` tinyint(1) NOT NULL DEFAULT ‘0‘,    ->   `parseurloff` tinyint(1) NOT NULL DEFAULT ‘0‘,    ->   `attachment` tinyint(1) NOT NULL DEFAULT ‘0‘,    ->   `rate` smallint(6) NOT NULL DEFAULT ‘0‘,    ->   `ratetimes` tinyint(3) unsigned NOT NULL DEFAULT ‘0‘,    ->   `status` int(10) NOT NULL DEFAULT ‘0‘,    ->   `tags` varchar(255) NOT NULL DEFAULT ‘0‘,    ->   `comment` tinyint(1) NOT NULL DEFAULT ‘0‘,    ->   `replycredit` int(10) NOT NULL DEFAULT ‘0‘,    ->   `position` int(8) unsigned NOT NULL AUTO_INCREMENT,    ->   PRIMARY KEY (`tid`,`position`),    ->   UNIQUE KEY `pid` (`pid`),    ->   KEY `fid` (`fid`),    ->   KEY `authorid` (`authorid`,`invisible`),    ->   KEY `dateline` (`dateline`),    ->   KEY `invisible` (`invisible`),    ->   KEY `displayorder` (`tid`,`invisible`,`dateline`),    ->   KEY `first` (`tid`,`first`)    -> ) ENGINE=MyISAM DEFAULT CHARSET=gbk    -> ;Query OK, 0 rows affected (0.04 sec)mysql> show tables;+------------------+| Tables_in_cangls |+------------------+| pre_forum_post   |+------------------+1 row in set (0.01 sec)mysql> show master status;+------------------+----------+--------------+------------------+| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+------------------+| mysql-bin.000032 |     1808 |              | mysql            |+------------------+----------+--------------+------------------+1 row in set (0.00 sec)從服務:mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || aming              || cangls             || mysql              || test               |+--------------------+5 rows in set (0.04 sec)mysql> use cangls;Database changedmysql> show tables;+------------------+| Tables_in_cangls |+------------------+| pre_forum_post   |+------------------+1 row in set (0.00 sec)mysql> show slave status\G;*************************** 1. row ***************************               Slave_IO_State: Waiting for master to send event                  Master_Host: 192.168.1.111                  Master_User: repl                  Master_Port: 3306                Connect_Retry: 60              Master_Log_File: mysql-bin.000032          Read_Master_Log_Pos: 1808               Relay_Log_File: localhost-relay-bin.000002                Relay_Log_Pos: 1953        Relay_Master_Log_File: mysql-bin.000032             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: 1808              Relay_Log_Space: 2112              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: 0Master_SSL_Verify_Server_Cert: No                Last_IO_Errno: 0                Last_IO_Error:               Last_SQL_Errno: 0               Last_SQL_Error:1 row in set (0.01 sec)ERROR:No query specified

 

mysql主從配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.