mysql+centos7+主從複製

來源:互聯網
上載者:User

標籤:uri   api   主從機   val   creat   mysqld   完全   管理系   收購   

MYSQL(mariadb)
MariaDB資料庫管理系統是MySQL的一個分支,主要由開源社區在維護,採用GPL授權許可。
開發這個分支的原因之一是:甲骨文公司收購了MySQL後,有將MySQL閉源的潛在風險,因此社區採用分支的方式來避開這個風險。
MariaDB的目的是完全相容MySQL,包括API和命令列,使之能輕鬆成為MySQL的代替品。
方法1:yum安裝mariadb

 

yum install mariadb-server mariadb 

相關命令

 
mariadb資料庫的相關命令是:systemctl start mariadb  #啟動MariaDBsystemctl stop mariadb  #停止MariaDBsystemctl restart mariadb  #重啟MariaDBsystemctl enable mariadb  #設定開機啟動
 

啟動後正常使用mysql

systemctl start mariadb#進入mysqlmysql -uroot -p

方法2:官網下載mysql-server包(rpm)

# wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm# rpm -ivh mysql-community-release-el7-5.noarch.rpm# yum install mysql-community-server
配置mysql

1.中文編碼設定

 
編輯mysql設定檔/etc/my.cnf[mysqld]character-set-server=utf8datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sockcollation-server=utf8_general_cisecurity riskssymbolic-links=0log-error=/var/log/mysqld.logpid-file=/var/run/mysqld/mysqld.pid[client]default-character-set=utf8[mysql]default-character-set=utf8
 

2.授權配置

 
遠端連線設定哦設定所有庫,所有表的所有許可權,賦值許可權給所有ip地址的root使用者
mysql > grant all privileges on *.* to [email protected]‘%‘ identified by ‘password‘;
#建立使用者
mysql > create user ‘username‘@‘%‘ identified by ‘password‘;
#重新整理許可權
flush privileges;
 MYSQL主從複製

主從機制實現原理

(1) master將改變記錄到二進位日誌(binary log)中(這些記錄叫做二進位日誌事件,binary log events); (2) slave將master的binary log events拷貝到它的中繼日誌(relay log); (3) slave重做中繼日誌中的事件,將改變反映它自己的資料。
master主庫配置 
#查看資料庫狀態systemctl status mariadb#停mariadbsystemctl stop mariadb

#修改設定檔
vim /etc/my.cnf
#修改內容
#解釋:server-id服務的唯一標識(主從之間都必須不同);log-bin啟動二進位日誌名稱為mysql-bin

  [mysqld]
  server-id=1
  log-bin=mysql-bin

#重啟mariadb
systemctl start mariadb
 master主庫添加從庫帳號 
1.建立用於主從同步的使用者chaoge,允許登入的從庫是‘192.168.178.130‘create user ‘chaoge‘@‘192.168.178.130‘ identified by ‘redhat‘;2.#題外話:如果提示密碼太簡單不複合策略加在前面加這句mysql> set global validate_password_policy=0;3.給從庫帳號授權,說明給chaoge從庫複製的許可權,在192.168.178.130機器上複製
grant replication slave on *.* to ‘chaoge‘@‘192.168.178.130‘;

4.檢查主庫的狀態

MariaDB [(none)]> show master status
-> ;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 575 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

File是二進位記錄檔名,Position 是日誌開始的位置。後面從庫會用到 後面從庫會用到 後面從庫會用到!!!!!!

 slave從庫配置 
1.檢查slave資料庫狀態systemctl status mariadb2.停止mariadbsystemctl stop mariadb

3.修改設定檔my.cnf
[mysqld]
server-id=2 #一定要和主庫區別開

4.重啟mariadb
systemctl start mariadb

5.登入mariadb
mysql -uroot -p

6.使用主庫授權的帳號

mysql > change master to master_host=‘192.168.178.129‘,
master_user=‘chaoge‘,
master_password=‘redhat‘,
master_log_file=‘mysql-bin.000001‘,
master_log_pos=575;

 7.重啟資料庫

systemctl restart mariadb

mysql+centos7+主從複製

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.