centos7 mysql主從數據庫同步

主:192.168.2.222;從:192.168.2.223html

一:安裝mysqlpython

從最新版本的linux系統開始,默認的是 Mariadb而不是mysql!這裏依舊以mysql爲例進行展現mysql

一、先檢查系統是否裝有mysqllinux

rpm -qa | grep mysqlc++

這裏返回空值,說明沒有安裝sql

二、下載mysql的repo源數據庫

# wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpmvim

安裝mysql-community-release-el7-5.noarch.rpm包centos

# sudo rpm -ivh mysql-community-release-el7-5.noarch.rpmbash

安裝這個包後,會得到兩個mysql的yum repo源:/etc/yum.repos.d/mysql-community.repo,/etc/yum.repos.d/mysql-community-source.repo。

 

三、安裝mysql

# sudo yum install mysql-server -y

根據步驟安裝就能夠了,不過安裝完成後,沒有密碼,須要重置密碼。

安裝後再次查看mysql

 

若是報錯,內容含有

Error: Package: mysql-community-libs-5.6.35-2.el7.x86_64 (mysql56-community)

           Requires: libc.so.6(GLIBC_2.17)(64bit)

Error: Package: mysql-community-server-5.6.35-2.el7.x86_64 (mysql56-community)

           Requires: libc.so.6(GLIBC_2.17)(64bit)

Error: Package: mysql-community-server-5.6.35-2.el7.x86_64 (mysql56-community)

           Requires: systemd

Error: Package: mysql-community-server-5.6.35-2.el7.x86_64 (mysql56-community)

           Requires: libstdc++.so.6(GLIBCXX_3.4.15)(64bit)

Error: Package: mysql-community-client-5.6.35-2.el7.x86_64 (mysql56-community)

           Requires: libc.so.6(GLIBC_2.17)(64bit)

 You could try using --skip-broken to work around the problem

 You could try running: rpm -Va --nofiles --nodigest

解決:

#yum install glibc.i686# yum list libstdc++*

四、重置密碼

重置密碼前,首先要登陸

# mysql -u root

登陸時有可能報這樣的錯:ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2),緣由是/var/lib/mysql的訪問權限問題。下面的命令把/var/lib/mysql的擁有者改成當前用戶:

# sudo chown -R openscanner:openscanner /var/lib/mysql

若是報chown: 無效的用戶: "openscanner:openscanner"錯誤,更換命令,並用 ll 查看目錄權限列表

chown root /var/lib/mysql/

ll

 

附: 
① 更改文件擁有者 (chown ) 
[root@linux ~]# chown 帳號名稱 文件或目錄 
② 改變文件的用戶組用命令 chgrp 
[root@linux ~]# chgrp 組名 文件或目錄 
③ 對於目錄權限修改以後,默認只是修改當前級別的權限。若是子目錄也要遞歸須要加R參數 
Chown -R : 進行遞歸,連同子目錄下的全部文件、目錄

而後,重啓服務:

service mysqld restart

接下來登陸重置密碼:

 mysql -u root 

mysql > use mysql;

mysql > update user set password=password('123456') where user='root';

mysql > exit;

重啓mysql服務後才生效 # service mysqld restart

必要時加入如下命令行,爲root添加遠程鏈接的能力。連接密碼爲 「root」(不包括雙引號)

mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "root"; 

報錯

【MySQL報錯】ERROR 1558 (HY000): Column count of mysql.user is wrong. Expected 43, found 39.

在網上查找緣由說說由於升級不當致使,執行如下命令便可正常執行命令

mysql_upgrade -uroot -p123456

 

六、查詢數據庫編碼格式,確保是 UTF-8

show variables like "%char%";

 

須要修改編碼格式爲UTF-8,導入數據庫sql的時候,請確保sql文件爲utf8編碼 
進入mysql命令行後 輸入

set names utf8;

(測試數據庫數據) 
再進入數據庫 use test; 
在導入sql腳本 source test.sql;

七、開放3306端口號 
firewalld 防火牆(centos-7)運行命令,並重啓:

firewall-cmd --zone=public --add-port=3306/tcp --permanentfirewall-cmd --reload

iptables 防火牆(centos6.5及其之前)運行命令

vim /etc/sysconfig/iptables

在文件內添加下面命令行,而後重啓

-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

# service iptables restart

外部連接訪問效果(通常創建sql數據庫和數據表,建議經過遠程連接控制,直觀易操做)

 

附:

出現「Warning: Using a password on the command line interface can be insecure.」的錯誤

第一種方法、修改數據庫配置文件 
一、咱們須要修改數據庫配置文件,這個要看咱們數據庫的配置的,有些是在/etc/my.cnf,有些是/etc/my.conf

 

咱們須要在[client]部分添加腳本:

  • socket=/var/lib/mysql/mysql.sock  ( mysql.sock 文件位置 )host=localhostuser=數據庫用戶password='數據庫密碼'

這裏參數要修改爲咱們本身的。

二、採用命令導出和導入數據庫 
其實在這個時候,咱們若是採用」詳解使用mysqldump命令備份還原MySQL數據用法整理http://www.laozuo.org/5047.html「介紹的方法也是可使用的,雖然依舊有錯誤提示,可是數據庫仍是能夠導出的。您確定和老左同樣是追求細節的人,一點點問題都不能有,但咱們能夠用下面的命令導出和導入,就沒有錯誤提示。

導出數據庫

mysqldump --defaults-extra-file=/etc/my.cnf database > database.sql

導入數據庫

mysql --defaults-extra-file=/etc/my.cnf database < database.sql

這裏咱們能夠看到上面的命令和之前經常使用的快速導入和導入命令有所不一樣了,須要加載咱們配置的MYSQL配置文件,這個「/etc/my.cnf」要根據咱們實際的路徑修改。用這樣的命令導出備份和導入是沒有錯誤提示的。

登錄數據庫

# mysql -u root -p

第二種方法、利用mysql_config_editor

一、設置加密模式

mysql_config_editor set --login-path=local --host=localhost --user=db_user --password

「db_user」是須要修改爲咱們本身數據庫用戶名的,回車以後會提示咱們輸入數據庫密碼,咱們照樣輸入。

二、執行備份

mysqldump -u db_user -pInsecurePassword my_database | gzip > backup.tar.gz

-u db_user 
-p.InsecurePassword ( 中間的「.」記得去掉 )

根據咱們數據信息修改用戶和用戶名和數據庫密碼,執行備份,這裏老左測試仍是有錯誤提示,但數據庫是能夠備份的。

修改MySQL的root用戶的密碼: 
mysql -u root mysql 
mysql>use mysql; 
mysql>desc user; 
mysql> GRANT ALL PRIVILEGES ON . TO root@」%」 IDENTIFIED BY 「root」;  //爲root添加遠程鏈接的能力。 
mysql>update user set Password = password(‘xxxxxx’) where User=’root’; 
mysql>select Host,User,Password from user where User=’root’; 
mysql>flush privileges; 
mysql>exit;

從新登陸:mysql -u root -p 
delete from mysql.user where user=」;  ← 刪除匿名用戶 
select user,host from mysql.user;  ← 查看用戶信息

二:master配置文件

vim /etc/my.cnf

[root@python mysql]# vim /etc/my.cnf

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
#MariaDB /etc/my.cnf.d/server.cnf
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-bin=master-bin
server-id=1
binlog-do-db = test
##同步cacti,此處關閉的話,就是除不容許的,其它的庫均同步。
#binlog-ignore-db = mysql        
##不一樣步mysql庫,如下同上
#binlog-ignore-db = test
#binlog-ignore-db = information_schema
# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

解釋[必定要加到【mysqld】下面!!]

重啓數據庫後

show variables like '%log_bin%';(查看log-bin是否開啓)

 

server-id = 1 
#master端ID號
 
log-bin=mysql-bin    
#日誌路徑及文件名
 
#binlog-do-db = cacti
#同步cacti,此處關閉的話,就是除不容許的,其它的庫均同步。
binlog-ignore-db = mysql        
#不一樣步mysql庫,如下同上
binlog-ignore-db = test
binlog-ignore-db = information_schema

 

爲從庫添加一個同步用的用戶

進入mysql

mysql>grant replication slave on *.* to sync@'192.168.1.239' identified by '123456';
mysql>flush privileges;

生成備份文件(或者使用第三方軟件heidisql)

生成一個備份文件, 從庫第一次要導入用的, 在導出以前要對數據庫進行鎖表,這樣能夠防止別人在我備份時對數據庫進行寫操做。注意: 當一個會話結束時它鎖的表會解除鎖表。

  • 先開一個會話登入數據庫後進行鎖表
mysql>flush tables with read lock;
  • 再開一個會話進行數據庫備份, 注意: mysql_bin_dir 是Mysql命令所在目錄
mysql_bin_dir/mysqldump --all-databases –E -u root -p > backup.sql
  • 立刻重啓數據庫,這樣在記主庫的Position會比較方便。
#service mysql restart
  • 查看主庫的 File 和Position
  • .mysql > show master status;

slave配置

server-id=2
relay-log=relay-bin
read-only=on
replicate-do-db=jason_test

 

解釋

server-id=2 值惟一

relay-log=relay-bin  記錄主從同步數據多日誌文件

read-only = on  從庫設置只讀權限

replicate-do-db = test 制定要同步多數據庫

還原數據(或者使用第三方軟件)

在用Master端數據庫還原Slave以前最好Slave是從新安裝過的, 這樣能減小很多麻煩, 特別是它上面有Master沒有的數據,這樣在還原時是不會被刪除的。若是不是剛重裝過的, 那就把原來的數據庫先刪除一下。

#mysql_bin_dir/mysql -u root -p < backup.sql
#mysql -uroot -p

配置鏈接主服務器的信息(注意符號

mysql>start slave; mysql> show slave status \G;

mysql> stop slave;

mysql> change master to

-> master_host='192.168.0.222',

->master_user='test',

->master_password='123456',

->master_log_file='master-bin.000001',

->master_log_pos=120;

mysql> start slave;   

mysql> show slave status \G;                     (查看從服務器狀態)

mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.2.222
                  Master_User: test
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 120
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 284
        Relay_Master_Log_File: master-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: jason_test
          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: 120
              Relay_Log_Space: 451
              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
                  Master_UUID: f0d10793-9144-11e8-90cd-08002709e8ef
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
1 row in set (0.00 sec)

ERROR: 
No query specified

查看上面的Slave_IO_Running: Yes 和Slave_SQL_Running: Yes 2個都爲yes則證實主從同步正常,若是有任一個顯示NO,則證實同步有問題。能夠查看數據庫日誌文件,裏面基本上會顯示出錯誤之處,根據錯誤一步一步排查,基本上均可以解決的。

接下來就能夠在主數據庫上刪減修改,而後看看從服務器的數據

相關文章
相關標籤/搜索