Ubuntu下安裝Mysql(5.5)和主從功能配置 html
通過幾天的折騰,終於把Mysql主從功能配置好了,其實配置很簡單,不過仍是遇到了各式各樣的錯誤,下面總結一下, mysql
在安裝以前,能夠先ping一下兩臺服務器是否能夠互相通訊, sql
在ubuntu下在線安裝Mysql相對比較簡單,輸入以下命令即可自動安裝完成。 數據庫
sudo apt-get install mysql-server ubuntu
安裝的過程當中若是沒法正常獲取源數據,可使用如下命令安裝更新ubuntu的數據源 服務器
sudo apt-get update ide
咱們能夠經過netstat -tap|grep mysql來查看系統是否已經有了mysql服務 測試
查詢mysql 是否在運行,如輸入一下命令 spa
service mysql status rest
mysql start/running, process 10786
Mysql遠程登陸測試
在主機上添加一個帳戶slave
進入mysql
輸入如下的命令
grant replication slave,reload,super on *.* to slave@xx.103.118.99 identified by 'slave' ;
其中"slave@xx.103.118.99"的slave是用戶名,密碼是'slave';
查看受權是否成功,能夠輸入如下命令:
select user ,host,password from mysql.user
在從機上進行測試,輸入:
mysql -u slave -h xx.103.118.92 –p
若成功,顯示以下:
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 38
Server version:5.5.28-0ubuntu0.12.04.3-log (Ubuntu)
Copyright (c) 2000, 2012, 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.
若出現以下錯誤,
ERROR 2003 (HY000): Can't connect to MySQL server on "host" (111)
須要修改主機的配置文件將地址綁定給註釋掉(/etc/mysql/my.cnf):
#bind-address = 127.0.0.1 <---註釋掉這一行就能夠遠程登陸了
Mysql的主從複製原理很簡單,就是從服務器讀取主服務器的binlog,而後根據binlog的數據庫操做記錄來更新數據庫。根據這一原理,能夠對my.cnf配置文件進行相應的修改
主要添加以下配置:
server-id=1
log_bin=/var/log/mysql/mysql-bin.log
binlog_do_db=test 要進行主從複製的數據庫
binlog_ignore_db=information_schema 不須要進行主從複製的數據庫
配置好後重啓mysql
sudo /etc/init.d/mysql restart
進入mysql,輸入:
mysql> flush privileges;
mysql> show master status;
+------------------+----------+--------------+--------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------+
| mysql-bin.000011 | 107 | test | information_schema |
+------------------+----------+--------------+--------------------+
1 row in set (0.00 sec)
從機配置:(/etc/mysql/my.cnf):
server-id=2(不能夠和主機id同樣)
log_bin=/var/log/mysql/mysql-bin.log
replicate-do-db=test 要進行主從複製的數據庫
replicate-ignore-db=information_shcema 不須要進行主從複製的數據庫
配置好後重啓mysql
sudo /etc/init.d/mysql restart
進入mysql,輸入
stop slave;
change master to
master_host='xx.103.118.92',master_user='slave',master_password='slave',master_log_file='log.000011',master_log_pos=107;
start slave;
show slave status\G;
若是出現:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
以上兩項都爲Yes,那說明沒問題了。
能夠在主機上進行數據操做,看看從機是否會自動同步
若在從機遇到下面的錯誤,主要是主從同步的問題,Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in
binary log index file'
能夠執行一下命令
stop slave;
reset slave;
start slave;
安裝成功後,我又從新安裝配置了幾回,發現若是沒有執行reset slave,問題就沒法解決。