1. 程式人生 > >Linux7/Centos7 Mariadb主從配置過程

Linux7/Centos7 Mariadb主從配置過程

top 使用 ppi 提示 本地yum源 mark oot testing 安裝包

環境:RedHat7 同樣適用於Centos7
(本文是在無網絡環境部署mariadb主從)

卸載Mysql
(防止mysql和mariadb沖突 )

停止服務:systemctl stop mysqld

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

卸載:

rpm -e mysql-server

rpm -e --nodeps mysql-libs

準備環境
查看磁盤掛載情況:df –h < 如果沒有則掛載系統盤:mount/dev/cdrom /media >

PS: 在虛擬機設置裏對以下步驟進行操作 如果有網絡yum源就不需要掛載系統盤
技術分享圖片
(如果開機自動掛載到桌面上[帶桌面的Linux系統],那麽需要卸載,然後再進行重新掛載)

卸載:umonut/dev/cdrom

掛載:mount/dev/cdrom /media

使用本地yum源:

配置本地yum:cd /etc/yum.repos.d/

創建一個文件(以repo結尾),如:yum.repo,文件內容如下:進行配置:
技術分享圖片
開始安裝
執行命令:yum -y install mariadb mariadb-server

拷貝文件:cp /usr/share/mysql/my-huge.cnf /etc/my.cnf

啟動mariadb服務並開機自動運行。命令如下:

systemctl start mariadb

systemctl enable mariadb
技術分享圖片
查看防火墻狀態:systemctl status firewalld

停止防火墻:systemctl stop firewalld

設置開機不啟用防火墻:systemctl disable firewalld

開始設置Mariadb數據庫, 執行腳本:/usr/bin/mysql_secure_installation

按照下面提示進行操作即可:

NOTE:RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB

SERVERS IN PRODUCTIONUSE! PLEASE READ EACH STEP CAREFULLY!

In order to log intoMariaDB to secure it, we‘ll need the current

password for the rootuser. If you‘ve just installed MariaDB,and

you haven‘t set the rootpassword yet, the password will be blank,

so you should just pressenter here.

Enter current passwordfor root (enter for none): 安裝後默認沒有root密碼,直接回車

OK, successfully usedpassword, moving on...

Setting the rootpassword ensures that nobody can log into the MariaDB

root user without theproper authorisation.

Set root password? [Y/n]Y

New password: 輸入root的新密碼

Re-enter new password: 新密碼確認

Password updated successfully!

Reloading privilegetables..

... Success!

By default, a MariaDBinstallation has an anonymous user, allowing anyone

to log into MariaDBwithout having to have a user account created for

them. This is intended only for testing, and tomake 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 shouldonly be allowed to connect from ‘localhost‘. This

ensures that someonecannot guess at the root password from the network.

Disallow root loginremotely? [Y/n] 關閉root遠程登錄 Y

... Success!

By default, MariaDBcomes with a database named ‘test‘ that anyone can

access. This is also intended only for testing, andshould be removed

before moving into aproduction environment.

Remove test database andaccess to it? [Y/n] 刪除test數據庫 Y

  • Dropping test database...

    ... Success!

  • Removing privileges on test database...

    ... Success!

Reloading the privilegetables will ensure that all changes made so far

will take effectimmediately.

Reload privilege tablesnow? [Y/n] 確定以上所有操作 Y

... Success!

Cleaning up...

All done! If you‘ve completed all of the above steps,your MariaDB

installation should nowbe secure.

Thanks for usingMariaDB!

配置MariaDB主從
修改vim /etc/my.cnf配置文件:

主節點不需要進行修改
技術分享圖片
從節點進行修改 server-id=2
技術分享圖片
PS:進行重啟從節點(slave):systemctl restart mariadb

在主節點上建立賬戶並且授權Slave

登錄MariaDB數據庫:mysql -uroot –proot

建立主從復制用戶並授權:

語法:

GRANT REPLICATION SLAVE ON .{所有權限} TO‘slave‘@‘%‘{用戶名為slave,%為任意地址} identified by ‘slave‘;

命令:

GRANT REPLICATION SLAVE ON . TO‘slave‘@‘%‘ identified by ‘slave‘;

查詢SQL(Master的狀態)命令:SHOW MASTER STATUS;
技術分享圖片
配置從節點SLAVE:(註意在從節點上執行)

登錄從服務器:mysql -uroot –proot進行配置:

語法:

CHANGE MASTER TO

MASTER_HOST=‘主節點的IP地址‘, MASTER_USER=‘主節點授權的用戶‘, MASTER_PASSWORD=‘主節點授權的用戶的密碼‘,MASTER_LOG_FILE=‘mysql-bin.000007‘,MASTER_LOG_POS=2197;

命令:

CHANGEMASTER TO MASTER_HOST=‘192.168.1.31‘,MASTER_USER=‘slave‘,MASTER_PASSWORD=‘slave‘,MASTER_LOG_FILE=‘mysql-bin.000007‘,MASTER_LOG_POS=2197;
PS:註意語法逗號前後不要用空格。

查看主從狀態驗證:

命令:show slave status\G;
技術分享圖片
授權遠程用戶root登錄:(主從都需要進行執行)

GRANT ALL PRIVILEGES ON . TO ‘root‘@‘%‘ IDENTIFIED BY ‘root‘ WITHGRANT OPTION;

FLUSH PRIVILEGES;

如果對此有興趣,請掃下面二維碼免費獲取更多詳情
技術分享圖片

Linux7/Centos7 Mariadb主從配置過程