怎樣實現(xiàn)MySQL主從搭建,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
成都創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供思禮網(wǎng)站建設(shè)、思禮做網(wǎng)站、思禮網(wǎng)站設(shè)計、思禮網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、思禮企業(yè)網(wǎng)站模板建站服務,10年思禮做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡服務。
一主單從
步驟:
1、配置master主機的my.cnf,配置內(nèi)容如下:
[mysqld]
user=mysql
pid-file=/var/run/mysqld/mysqld.pid
socket=/var/run/mysqld/mysqld.sock
port=3306
basedir=/usr
datadir=/var/lib/mysql
tmpdir=/tmp
log-bin=master-bin
log-bin-index=master-bin.index
server-id=1
配置完之后重啟,使my.cnf生效
創(chuàng)建復制用戶,并賦予replication slave權(quán)限給slave節(jié)點
mysql> create user repl_user;
mysql> grant replication slave on *.* to repl_user@mt identified by xyzzy;
2、配置slave從機的my.cnf,配置內(nèi)容如下:
[mysqld]
user=mysql
pid-file=/var/run/mysqld/mysqld.pid
socket=/var/run/mysqld/mysqld.sock
port=3306
basedir=/usr
datadir=/var/lib/mysql
tmpdir=/tmp
server-id=2
relay-log-index=slave-relay-bin.index
relay-log=slave-relay-bin
在slave上配置slave與master的連接
change master to
master_host='mo',
master_port='3306',
master_user'repl_user',
master_password='xyzzy',
master_log_file='mysql-bin.00002',
master_log_pos=101;
啟動slave
start slave
3、查看slave的狀態(tài)
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: mo
Master_User: repl_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 946
Relay_Log_File: slave-relay-bin.000002
Relay_Log_Pos: 1092
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: 946
Relay_Log_Space: 1247
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:
1 row in set (0.00 sec)
如果Slave_IO_Running、Slave_SQL_Running均為yes則證明配置成功,如果有其中一個為no,如下所示:
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: mo
Master_User: repl_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File:
Read_Master_Log_Pos: 4
Relay_Log_File: slave-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File:
Slave_IO_Running: No
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: 0
Relay_Log_Space: 106
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: 2013
Last_IO_Error: error connecting to master 'repl_user@mo:3306' - retry-time: 60 retries: 86400
Last_SQL_Errno: 0
Last_SQL_Error:
1 row in set (0.00 sec)
則查看Last_IO_Errno、Last_IO_Error參數(shù),和datadir目錄下的hostname.err文件找出錯誤日志信息,如下所示
[root@mt ~]# cd /var/lib/mysql/
[root@mt mysql]# ls
ibdata1 ib_logfile0 ib_logfile1 master.info mt.err mysql relay-log.info slave-relay-bin.000001 slave-relay-bin.index test
[root@mt mysql]# cd mt.err
-bash: cd: mt.err: Not a directory
[root@mt mysql]# tail -f n 200 mt.err
tail: cannot open `n' for reading: No such file or directory
tail: cannot open `200' for reading: No such file or directory
==> mt.err <==
160731 19:27:43 InnoDB: Initializing buffer pool, size = 8.0M
160731 19:27:43 InnoDB: Completed initialization of buffer pool
160731 19:27:43 InnoDB: Started; log sequence number 0 44233
160731 19:27:43 [Note] Event Scheduler: Loaded 0 events
160731 19:27:43 [Note] /usr/libexec/mysqld: ready for connections.
Version: '5.1.71' socket: '/var/run/mysqld/mysqld.sock' port: 3306 Source distribution
160731 19:47:02 [Note] 'CHANGE MASTER TO executed'. Previous state master_host='', master_port='3306', master_log_file='', master_log_pos='4'. New state master_host='mo', master_port='3306', master_log_file='', master_log_pos='4'.
160731 19:47:09 [Note] Slave SQL thread initialized, starting replication in log 'FIRST' at position 0, relay log './slave-relay-bin.000001' position: 4
160731 19:47:09 [ERROR] Slave I/O: error connecting to master 'repl_user@mo:3306' - retry-time: 60 retries: 86400, Error_code: 2005
160731 21:13:09 [ERROR] Slave I/O: error connecting to master 'repl_user@mo:3306' - retry-time: 60 retries: 86400, Error_code: 2013
上述錯誤為repl_user沒有給slave機replication slave權(quán)限
可以查看select user,host,password from mysql.user;看到user在哪個host上可以登陸,如果repl_user對應的host沒有slave節(jié)點對應的主機名,則repl_user用戶無法在slave機上進行遠程連接到master所在的節(jié)點機。
可以使用show binary logs查看logbin的日志信息
可以使用show master status \G 查看主節(jié)點信息
關(guān)于怎樣實現(xiàn)MYSQL主從搭建問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。
新聞標題:怎樣實現(xiàn)MYSQL主從搭建
分享鏈接:http://muchs.cn/article30/gdddpo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、企業(yè)建站、定制開發(fā)、網(wǎng)站維護、軟件開發(fā)、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)