這篇文章將為大家詳細(xì)講解有關(guān)MySQL中怎么創(chuàng)建數(shù)據(jù)庫并配置主從,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
目前創(chuàng)新互聯(lián)已為上千余家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、綿陽服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計(jì)、訥河網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
1、主服務(wù)器上創(chuàng)建一個(gè)用于復(fù)制的賬戶。
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.101.3' IDENTIFILED BY 'Mysqlrepl';
mysql> flush privileges;
2、主服務(wù)器參數(shù)修改
[root@localhost ~]# vi /usr/my.cnf
修改如下內(nèi)容
server-id = 1
log-bin=mysql-bin
3、主服務(wù)器備份數(shù)據(jù),并傳輸
mysql> flush tables with read lock;
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.00002 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
[root@localhost ~]# mysqldump -uroot -p --all-databases | gzip > export_t.sql.gz
[root@localhost ~]# scp export_t.sql.gz root@192.168.101.3:/tmp/
mysql> unlock tables;
4、從服務(wù)器恢復(fù)數(shù)據(jù)
[root@localhost bin]# gunzip export_t.sql.gz
[root@localhost bin]# mysql -uroot -p
mysql> source /tmp/export_t.sql.gz;
5、修改從服務(wù)器配置
[root@localhost bin]# vi /usr/my.cnf
添加如下內(nèi)容
server-id = 2
重啟服務(wù)器
[root@localhost ~]# service mysql restar
6、從服務(wù)器設(shè)置主從配置
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.101.5',
-> MASTER_USER='repl',
-> MASTER_PASSWORD='Mysqlrepl',
-> MASTER_LOG_FILE='mysql-bin.00002',
-> MASTER_PORT=3306,
-> MASTER_LOG_POS=120;
MASTER_HOST指的是主服務(wù)器的IP地址,
MASTER_USER指的是復(fù)制的賬戶
MASTER_PASSWORD指的是賬戶的密碼
MASTER_PORT指的是主服務(wù)器端口
MASTER_LOG_FILE指的是bin-log的文件
MASTER_LOG_POS指的是日志文件位
7、從服務(wù)器啟動slave線程
mysql> start slave;
mysql> show processlist;
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+-----------+---------------+
| Id | User | Host | db | Command | Time | State | Info | Rows_sent | Rows_examined |
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+-----------+---------------+
| 1 | system user | | NULL | Connect | 714 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL | 0 | 0 |
| 2 | system user | | NULL | Connect | 714 | Waiting for master to send event | NULL | 0 | 0 |
| 4 | root | localhost | NULL | Query | 0 | init | show processlist | 0 | 0 |
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+-----------+---------------+
3 rows in set (0.08 sec)
這已經(jīng)表明主從已經(jīng)搭建成功。
8、測試
主服務(wù)器中
mysql> use test
Database changed
mysql> select * from aaa;
+------+------+
| a | b |
+------+------+
| 1 | 2 |
| 1 | 2 |
| 1 | 2 |
| 2 | 3 |
| 2 | 7 |
+------+------+
5 rows in set (0.06 sec)
從服務(wù)器中
mysql> use test;
Database changed
mysql> select * from aaa;
+------+------+
| a | b |
+------+------+
| 1 | 2 |
| 1 | 2 |
| 1 | 2 |
| 2 | 3 |
| 2 | 7 |
+------+------+
5 rows in set (0.21 sec)
主服務(wù)器中
mysql> insert into aaa values (5,7),(56,21);
Query OK, 2 rows affected (0.21 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from aaa;
+------+------+
| a | b |
+------+------+
| 1 | 2 |
| 1 | 2 |
| 1 | 2 |
| 2 | 3 |
| 2 | 7 |
| 5 | 7 |
| 56 | 21 |
+------+------+
7 rows in set (0.00 sec)
從服務(wù)器中
mysql> select * from aaa;
+------+------+
| a | b |
+------+------+
| 1 | 2 |
| 1 | 2 |
| 1 | 2 |
| 2 | 3 |
| 2 | 7 |
| 5 | 7 |
| 56 | 21 |
+------+------+
7 rows in set (0.00 sec)
9、管理主從
查看從服務(wù)器狀態(tài)
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.101.5
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.00002
Read_Master_Log_Pos: 120
Relay_Log_File: localhost-relay-bin.000001
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000036
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: 120
Relay_Log_Space: 460
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
Master_UUID: 86d34969-fa5d-11e6-b372-000c29c88c3f
Master_Info_File: /usr/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
關(guān)心的兩個(gè)信息是Slave_IO_Running和Slave_SQL_Running是否是YES。Slave_IO_Running是從主服務(wù)器讀取BINLOG日志,并寫入到從服務(wù)器的中繼日志中;Slave_SQL_Running負(fù)責(zé)讀取和執(zhí)行中繼日志信息。
注意:如果需要從服務(wù)器禁止寫入操作,則需要更改參數(shù)read-only,使從服務(wù)器非root賬戶只能讀數(shù)據(jù)。
關(guān)于Mysql中怎么創(chuàng)建數(shù)據(jù)庫并配置主從就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
新聞名稱:Mysql中怎么創(chuàng)建數(shù)據(jù)庫并配置主從
網(wǎng)頁鏈接:http://jinyejixie.com/article22/pgiicc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)、微信公眾號、關(guān)鍵詞優(yōu)化、網(wǎng)站設(shè)計(jì)、網(wǎng)站排名、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)