建立MySQL從庫,建立MySQL

來源:互聯網
上載者:User

建立MySQL從庫,建立MySQL

我們知道Oracle有DataGuard即時備份資料,可以做主備切換,而MySQL也有自己的一套備庫方案,稱之為主從複製。


搭建MySQL從庫是為了即時同步主庫資料,同時也可以分擔主庫的讀壓力,對資料庫端做成讀寫分離結構。


搭建MySQL主從庫注意點:


1.主庫和從庫的 server-id 一定不能相同。


2.在主庫建立replication slave賬戶。


grant replication slave on *.* to 'repl'@'192.168.0.232' identified 'oracle';


3.查看主庫master狀態

mysql> show master status \G
*************************** 1. row ***************************
            File: mysql-bin.000005
        Position: 251651
    Binlog_Do_DB: 
Binlog_Ignore_DB: 
1 row in set (0.00 sec)


4.配置從庫


 change master to
    -> master_host='192.168.0.232',
    -> master_user='repl',
    -> master_password='oracle',
    -> master_log_file='mysql-bin.000005',
    -> master_log_pos=251651;


5. 啟動從庫

slave start


show slave status\G


*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: ***********
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000005
          Read_Master_Log_Pos: 463725968
               Relay_Log_File: mysql-relay-bin.000006
                Relay_Log_Pos: 463726114
        Relay_Master_Log_File: mysql-bin.000005
             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: 463725968
              Relay_Log_Space: 873569451
              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: 100



注意:

如果從庫Slave_IO_Running: No/ Slave_SQL_Running: No

關閉slave

        設定set GLOBALSQL_SLAVE_SKIP_COUNTER=1;
在開啟slave




建立的mysql資料庫預設儲存在哪?

如果沒有自己去設定安裝路徑,MYSQL預設安裝在C:\Program Files\MySQL\MySQL Server 5.1,建立的資料庫檔案在C:\Documents and Settings\All Users\Application Data\MySQL\MySQL Server 5.1\data這裡。
 
mysql資料庫怎建詳細點?

1、使用SHOW語句找出在伺服器上當前存在什麼資料庫:
mysql> SHOW DATABASES;
+----------+
| Database |
+----------+
| mysql |
| test |
+----------+
3 rows in set (0.00 sec)

2、建立一個資料庫abccs
mysql> CREATE DATABASE abccs;
注意不同作業系統對大小寫敏感。
3、選擇你所建立的資料庫
mysql> USE abccs
Database changed
此時你已經進入你剛才所建立的資料庫abccs.
4、 建立一個資料庫表
首先看現在你的資料庫中存在什麼表:
mysql> SHOW TABLES;
Empty set (0.00 sec)
說明剛才建立的資料庫中還沒有資料庫表。下面來建立一個資料庫表mytable: 我們要建立一個你公司員工的生日表,表的內容包含員工姓名、性別、出生日期、出生城市。
mysql> CREATE TABLE mytable (name VARCHAR(20), sex CHAR(1),
-> birth DATE, birthaddr VARCHAR(20));
Query OK, 0 rows affected (0.00 sec)

由於name、birthadd的列值是變化的,因此選擇VARCHAR,其長度不一定是20。可以選擇從1到255的任何長度,如果以後需要改變它的字長,可以使用ALTER TABLE語句。);性別只需一個字元就可以表示:"m"或"f",因此選用CHAR(1);birth列則使用DATE資料類型。
建立了一個表後,我們可以看看剛才做的結果,用SHOW TABLES顯示資料庫中有哪些表:
mysql> SHOW TABLES;
+---------------------+
| Tables in menagerie |
+---------------------+
| mytables |
+---------------------+

5、顯示表的結構:
mysql> DESCRIBE mytable;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
| birth | date | YES | | NULL | |
| deathaddr | varchar(20) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

6、 往表中加入記錄
我們先用SELECT命令來查看錶中的資料:
mysql> select * from mytable;
Empt......餘下全文>>
 

相關文章

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.