兩臺服務器
主:172.16.0.120 Master120
從:172.16.0.121 backup121
兩臺都要安裝mysql如下步驟
安裝系統所需要的依賴包
[root@ Master120 ~]# yum -y install gcc gcc-c++ gcc-g77 autoconf automake zlib* fiex* libxml* ncurses-devel libmcrypt* libtool-ltdl-devel* cmake bison
創建mysql用戶與用戶組
[root@ Master120 ~]# groupadd mysql
[root@ Master120 ~]# useradd -r -g mysql mysql
安裝mysql
[root@ Master120 ~]# tar zxvf mysql-5.5.25.tar.gz
[root@ Master120 ~]# cd mysql-5.5.25
[root@Master120 mysql-5.5.25]# cmake -DCMAKE_INSTALL_PREFIX=/opt/mysql/ -DMYSQL_DATADIR=/opt/mysql/data -DMYSQL_UNIX_ADDR=/opt/mysql/data/mysqld.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_TCP_PORT=3306 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_UNIX_ADDR=/opt/mysql/data/mysql.sock -DMYSQL_USER=mysql -DWITH_DEBUG=0
[root@ Master120 mysql-5.5.25]# make && make install
賦予相關權限
[root@ Master120 mysql-5.5.25]# chown -R mysql:mysql /opt/mysql
[root@Master120 mysql-5.5.25]# /opt/mysql/scripts/mysql_install_db --user=mysql --basedir=/opt/mysql --datadir=/opt/mysql/data
mysql配置文件
[root@ Master120 mysql-5.5.25]# cp /opt/mysql/support-files/my-large.cnf /etc/my.cnf
[root@Master120 mysql-5.5.25]# cp /opt/mysql/support-files/mysql.server /etc/init.d/mysql
[root@Master120 mysql-5.5.25]# chmod +x /etc/init.d/mysql
增加自動啓動
[root@Master120 mysql-5.5.25]# chkconfig mysql on
啓動mysql
[root@Master120 mysql-5.5.25]# /etc/init.d/mysql start
Starting MySQL...... [ OK ]
查看mysql 的3306端口,看到下面說明啓動成功
[root@Master120 mysql-5.5.25]#ps aux | grep 3306
mysql 17237 3.6 8.4 615660 86264 pts/2 Sl 22:10 0:01 /opt/mysql/bin/mysqld --basedir=/opt/mysql --datadir=/opt/mysql/data --plugin-dir=/opt/mysql/lib/plugin --user=mysql --log-error=/opt/mysql/data/Master120.err --pid-file=/opt/mysql/data/Master120.pid --socket=/opt/mysql/data/mysql.sock --port=3306
root 17798 0.0 0.0 61228 784 pts/2 S+ 22:10 0:00 grep 3306
設置mysql初始密碼,123456是密碼,你可以設置自己需要的密碼
[root@Master120 mysql-5.5.25]# /opt/mysql/bin/mysqladmin -u root password '123456'
[root@Master120 mysql-5.5.25]# /opt/mysql/bin/mysql -uroot -p123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.25-log Source distribution
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
主從複製的配置
主的my.cnf的server-id=1,每個同步服務都必須設定一個唯一的編號。
在Master(這裏爲Master120機器)上增加一個用於複製的賬號:
mysql>GRANT REPLICATION SLAVE ON *.* TO 'repl'@'172.16.0.%' IDENTIFIED BY '123456';
mysql>FLUSH REPLICATION;
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 245 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
設置Slave 主機
修改my.cnf的server-id,內容如下:
backup121主機
server-id=2
開啓Master與Slave的同步
在Slave上執行如下命令
mysql>CHANGE MASTER TO MASTER_HOST='172.16.0.120',
->MASTER_USER='repl',
->MASTER_PASSWORD='123456',
->MASTER_LOG_FILE='mysql-bin.000001',
->MASTER_LOG_POS=245;
之後執行
mysql>slave start;
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.0.120
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 245
Relay_Log_File: backup121-relay-bin.000001
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-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: 245
Relay_Log_Space: 556
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
1 row in set (0.00 sec)
看到都爲yes,說明成功
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
注意防火牆,關閉防火牆或者增加3306端口對外訪問
如果在生產庫中從庫同步出錯我們可以在從庫的my.cnf增加Slave_skip_errors =all 跳過全部錯誤。