天天看點

CentOS6.8配置Mysql資料庫主從複制

Centos6.8配置MySql5.1資料庫主從複制

資料庫安裝:

1、 系統資訊:

1.  [[email protected] local]# cat /proc/version

2.  Linux version 2.6.32-642.el6.x86_64([email protected]) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-17)(GCC) ) #1 SMP Tue May 10 17:27:01 UTC 2016

3.  CentOS 6.8 64位CentOS-6.8-x86_64-bin-DVD1.iso

2、 安裝MySql資料庫:如果有沖突:

4.  ##[[email protected] develop]# yum remove mysql-libsCentos中徹底删除Mysql(rpm、yum安裝的情況)

5.  安裝mysql資料庫(預設的為5.1):yum install -ymysql-server mysql mysql-deve

6.   檢視安裝資料庫資訊:rpm -qi mysql-server

啟動MySql服務

7.  [[email protected] ~]# /etc/init.d/mysql star

配置root使用者密碼

8.  /usr/bin/mysqladmin -u root password '密碼'

允許遠端通路:

9.  GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY '密碼' WITH GRANT OPTION;

10.  FLUSH PRIVILEGES 重新整理權限

###########################################################################

簡單的master配置:

11.  vi  /etc/my.cnf

12.  [mysqld]

13.  datadir=/var/lib/mysql

14.  socket=/var/lib/mysql/mysql.sock

15.  user=mysql

16.  # Disablingsymbolic-links is recommended to prevent assorted security risks

17.  symbolic-links=0

# 設定伺服器ID

18.  server-id=153

# 設定需要寫日志的資料庫

19.  binlog-do-db=test

# 設定不需要寫日志的資料庫

20.  binlog-ignore-db=mysql

# 日志基于行模式

21.  # binlog_format=row

# 二進制日志檔案存放位置

log-bin=mysql-bin

22.  [mysqld_safe]

23.  log-error=/var/log/mysqld.log

24.  pid-file=/var/run/mysqld/mysqld.pid

進入sql指令行:

25.  mysql> show masterstatus;

26.  +------------------+----------+--------------+------------------+

27.  | File             | Position | Binlog_Do_DB |Binlog_Ignore_DB |

28.  +------------------+----------+--------------+------------------+

29.  | mysql-bin.000004 |      696 | test         | mysql            |

30.  +------------------+----------+--------------+------------------+

31.  1 row in set (0.00 sec)

mysql>

紅色部分是必須修改的部分

32.  # 建立slave帳号slave,密碼123456  

33.  mysql>grant replication slave on *.* to 'slave'@'%' identified by '123456';  

34.  # 允許主庫可以被從庫使用該賬号遠端通路

35.  GRANT ALL PRIVILEGES ON*.* TO 'slave'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;

36.  FLUSH PRIVILEGES 重新整理權限

37.  service mysqld restart    重新開機資料庫服務

簡單的slave配置:

38.  [mysqld]

39.  datadir=/var/lib/mysql

40.  socket=/var/lib/mysql/mysql.sock

41.  user=mysql

42.  # Disablingsymbolic-links is recommended to prevent assorted security risks

43.  symbolic-links=0

44.  server-id=130

45.  [mysqld_safe]

46.  log-error=/var/log/mysqld.log

47.  pid-file=/var/run/mysqld/mysqld.pid

重新開機mysql服務

進入sql指令行:

1.  mysql> show slave status\G;

2.  *************************** 1. row***************************

3.                 Slave_IO_State: Waiting formaster to send event

4.                    Master_Host: 192.168.100.153

5.                    Master_User: slave

6.                    Master_Port: 3306

7.                  Connect_Retry: 60

8.                Master_Log_File: mysql-bin.000004

9.            Read_Master_Log_Pos: 696

10.                Relay_Log_File: mysqld-relay-bin.000009

11.                 Relay_Log_Pos: 636

12.         Relay_Master_Log_File: mysql-bin.000004

13.              Slave_IO_Running: Yes         #這兩項必須是yes才行(此為no是可能是主庫沒關防火牆)

14.             Slave_SQL_Running: Yes         #這兩項必須是yes才行

15.               Replicate_Do_DB:

16.           Replicate_Ignore_DB:

17.            Replicate_Do_Table:

18.        Replicate_Ignore_Table:

19.       Replicate_Wild_Do_Table:

20.   Replicate_Wild_Ignore_Table:

21.                    Last_Errno: 0

22.                    Last_Error:

23.                  Skip_Counter: 0

24.           Exec_Master_Log_Pos: 696

25.               Relay_Log_Space: 792

26.               Until_Condition: None

27.                Until_Log_File:

28.                 Until_Log_Pos: 0

29.            Master_SSL_Allowed: No

30.            Master_SSL_CA_File:

31.            Master_SSL_CA_Path:

32.               Master_SSL_Cert:

33.             Master_SSL_Cipher:

34.                Master_SSL_Key:

35.         Seconds_Behind_Master: 0

36. Master_SSL_Verify_Server_Cert: No

37.                 Last_IO_Errno: 0

38.                 Last_IO_Error:

39.                Last_SQL_Errno: 0

40.                Last_SQL_Error:

41. 1 row in set (0.00 sec)

42.  

43. ERROR:

44. No query specified

45.  

  1. mysql>

紅色部分是不許修改的内容

47. # 執行同步指令,設定主資料庫ip,同步帳号密碼,同步位置  

48. mysql>change master to master_host='192.168.100.153',master_user='slave',master_password='123456',master_log_file=' mysqld-relay-bin.000009',master_log_pos=636;  

49. # 開啟同步功能  

50. mysql>start slave;

51. # 停止主從同步  

52. mysql> stop slave;  

53. # 連接配接斷開時,重新連接配接逾時時間  

54. mysql> change master to master_connect_retry=50;    

55. # 開啟主從同步  

56. mysql> start slave; 

附錄:

關閉防火牆

1、重新開機後永久性生效:

開啟:chkconfig iptables on

關閉:chkconfig iptables off

2、即時生效,重新開機後失效:

開啟:service iptables start

關閉:service iptables stop

繼續閱讀