1. 程式人生 > 資料庫 >MariaDB(mysql)-主從搭建

MariaDB(mysql)-主從搭建

環境:Linux7

搭建MariaDB主從,裝備兩個乾淨的虛擬機器,分別安裝(不要安裝一個後去克隆,會出現不可控的問題出現)

mysql主從大忌:從節點不允許修改資料(從節點只讀不寫)

解除安裝過程:

停止服務:systemctl stop mariadb

查詢安裝包:rpm -qa | grep mariadb

解除安裝:(查到什麼,就解除安裝什麼)

rpm -e mariadb-server

rpm -e mariadb

rpm -e --nodeps mariadb-libs

 

一、準備環境

1.1 檢視磁碟掛載情況:df -h <如果沒有則掛載系統盤:mount/dev/cdrom/media>

    PS:在虛擬機器設定裡,對以下步驟進行操作:安裝系統的ISO映象檔案

    如果開機自動掛載到桌面上【帶桌面的Linux系統】,那麼需要解除安裝,然後再重新進行掛載

    解除安裝:umonut /dev/cdrom

    掛載:monut /dev/cdrom /media          【*中間是有空格的*】

1.2 使用本地yum源:

    配置本地yum:

    #cd /etc/yum.repos.d/

    建立一個檔案(以repo結尾),如:yum.repo,檔案內容如下:進行配置:

    #vim yum.repo

:wq    【儲存退出】

二、開始安裝

2.1 執行命令

    #yum -y install mariadb mariadb-server

2.2 拷貝檔案

    #cp /usr/share/mysql/my-huge.cnf /etc/my.cnf

2.3 設定表名不區分大小寫 

    用root賬號登入後,在/etc/my.cnf中的【mysql】後新增lower_case_table_names=1,重啟MySQL服務,這時已經設定成功,不區分表名的大小寫

2.4 啟動MariaDB服務並開機自動執行,命令如下:

   #systemctl start mariadb        [啟動]

   #systemctl enable mariadb    [開機自動啟動]

2.5 防火牆命令

     檢視防火牆狀態:systemctl status firewalld

     停止防火牆:systemctl stop firewalld

     設定開機不啟用防火牆:systemctl disable firewalld

2.6 開始設定mariadb資料庫

    執行指令碼:/usr/bin/mysql_secure_installation

[root@192 etc]# /usr/bin/mysql_secure_installation
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

註釋:安裝後預設沒有root密碼,直接回車
Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

註釋:設定root密碼,Y
Set root password? [Y/n] y 
註釋:輸入root新密碼
New password:
註釋:新密碼確認
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

註釋:刪除匿名使用者(可自定義)
Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

註釋:關閉root遠端登入[如果關閉,需要在配置中開啟]
Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

註釋:刪除test資料庫
Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

註釋:確定以上所有操作
Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

三、配置MariaDB主從

3.1 修改 vim /etc/my.cnf配置檔案

    *主節點不需要修改

    從節點進行修改server-id=2   [如果有多個從機,只要不是1的正整數,不重複]

    PS:從節點修改後,需重啟從節點(slave):systemctl restart mariadb

3.2 在主節點上建立賬戶(slave)並且授權   

    登入mariadb資料庫:mysql -uroot -pxxx    [-u使用者名稱 -p密碼     寫使用者名稱/密碼前沒有空格 ]

    建立主從複製的使用者並授權:

    語法:

    GRANT REPLICATION SLAVE ON *.*{所有許可權} TO 'slave'@'%'{使用者名稱為slave,%為任意地址} IDENTIFIED BY 'slave';

    命令:GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY 'slave';

3.3 查詢SQL(master的狀態),命令:SHOW MASTER STATUS;

3.4 配置從節點SLAVE:(注意在從節點上執行)

登入從伺服器:mysql -uroot -pxxx進行配置

語法(注意語法英文逗號(,)並且逗號前後沒有空格):

CHANGE MASTER TO MASTER_HOST='主節點的IP地址',MASTER_USER='主節點授權的使用者',MASTER_PASSWORD='主節點授權的使用者密碼',MASTER_LOG_FILE='主節點的File檔名',MASTER_LOG_POS=主機點的Position;

命令:CHANGE MASTER TO MASTER_HOST='192.168.0.10',MASTER_USER='slave',MASTER_PASSWORD='slave',MASTER_LOG_FILE='mysql-bin.000004',MASTER_LOG_POS=1613;

再執行命令:

    stop slave; //停止從服務

    start slave;//啟動從服務

3.5 檢視主從狀態驗證:

    命令:show slave status\G;

注:看到這兩項都是Yes,說明主從已配置成功

問題:如果這兩項顯示No,並且保證了之前的配置是正常的,那就重啟從節點MariDB(systemctl restart mariadb),再看下

3.6 授權遠端使用者root登入(主從都需要進行執行)

    命令:

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

        FLUSH PRIVILEGES;

 

 

常見問題:

    問題1:如果連線mariadb時,報錯:1045-Access denied for user 'root'@'本機IP' (usering password : YES)

    解決方案:

        1、登入資料:mysql -uroot -pxxx

        2、進入mysql庫(切庫):use mysql;

        3、重新設定root使用者的密碼:update user set password=password("123") where user="root";

        4、重新整理許可權:flush privileges;

        5、退出:exit;

        6、重啟:systemctl restart mariadb

      *********  問題解決*********

    問題2:如果從節點被修改,造成了主從不能同步時,修復操作:

        解決辦法1:

        Slave_SQL_Running:No,程式可能在從節點進行了寫操作,也可能是slave機器重啟後,事務回滾造成的。

        一般事務回滾造成的:

        mysql>stop slave;

        mysql>set  GLOBAL SQL_SLAVE_SKIP_COUNTER=1;

        mysql>start slave;

        解決辦法2:

            1、停止slave服務:slave stop;

            2、到主伺服器檢視主機狀態:show mastar start;

            3、記錄File和Position對應的值

            4、重複設定上面的從節點

注意:手動同步需要停止master的寫操作!!