MariaDB(MySQL)_MariaDB(Mysql)-主从搭建
卸載過程:
停止服務:systemctl stop mariadb
查詢安裝包:rpm -qa | grep mariadb
卸載:
rpm -e mariadb-server
rpm -e mariadb
rpm -e --nodeps mariadb-libs
1.?準備環境
1.1?查看磁盤掛載情況:df–h
PS: 在虛擬機設置里?對以下步驟進行操作:
(如果開機自動掛載到桌面上[帶桌面的Linux系統],那么需要卸載,然后再進行重新掛載)
卸載:umonut/dev/cdrom
掛載:mount /dev/cdrom /media
1.1?使用本地yum源:
配置本地yum:cd /etc/yum.repos.d/
創建一個文件(以repo結尾),如:yum.repo,文件內容如下:進行配置:
1.?開始安裝
1.1?執行命令:yum -y install mariadb mariadb-server
1.2?拷貝文件:cp /usr/share/mysql/my-huge.cnf /etc/my.cnf
1.3?用root帳號登錄后,在/etc/my.cnf中的[mysqld]后添加添加lower_case_table_names=1,重啟MYSQL服務,這時已設置成功:不區分表名的大小寫。
1.4?啟動mariadb服務并開機自動運行。命令如下:
systemctl start mariadb
systemctl enable mariadb
1.1?查看防火墻狀態:systemctl status firewalld
停止防火墻:systemctl stop firewalld
設置開機不啟用防火墻:systemctldisablefirewalld
1.2?開始設置Mariadb數據庫,執行腳本:/usr/bin/mysql_secure_installation
按照下面提示進行操作即可:
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.
Enter current password for root (enter for none): 安裝后默認沒有root密碼,直接回車
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] Y
New password: 輸入root的新密碼
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.
Disallow root login remotely? [Y/n] 關閉root遠程登錄 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.
Remove test database and access to it? [Y/n] 刪除test數據庫 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!
1.?配置MariaDB主從
1.1?修改vim /etc/my.cnf配置文件:
主節點不需要進行修改
從節點進行修改 server-id=2
PS:進行重啟從節點(slave):systemctl restart mariadb
1.1?在主節點上建立賬戶并且授權Slave
登錄MariaDB數據庫:mysql -uroot -proot
建立主從復制用戶并授權:
語法:
GRANT REPLICATION SLAVE ON *.*{所有權限}TO 'slave'@'%'{用戶名為slave,%為任意地址}identified by'slave';
命令:
GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%'identified by'slave';
1.2?查詢SQL(Master的狀態)命令:SHOW MASTER STATUS;
1.1?配置從節點SLAVE:(注意在從節點上執行)
登錄從服務器:mysql -u root –proot進行配置:
語法:
CHANGE MASTER TO
MASTER_HOST='主節點的IP地址', MASTER_USER='主節點授權的用戶', MASTER_PASSWORD='主節點授權的用戶的密碼',MASTER_LOG_FILE='mysql-bin.000007',MASTER_LOG_POS=2197;
命令:
CHANGE MASTER TO MASTER_HOST='192.168.1.31',MASTER_USER='slave',MASTER_PASSWORD='slave',MASTER_LOG_FILE='mysql-bin.000007',MASTER_LOG_POS=2197;
PS:注意語法逗號前后不要用空格。
1.2?查看主從狀態驗證:
命令:show slave status\G;
1.1?授權遠程用戶root登錄:(主從都需要進行執行)
1.1.1?GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
1.1.2?FLUSH PRIVILEGES;
進入slave服務器,運行:
MySQL> show slave status\G
.......
Relay_Log_File: localhost-relay-bin.000535
Relay_Log_Pos: 21795072
Relay_Master_Log_File: localhost-bin.000094
Slave_IO_Running: Yes
Slave_SQL_Running:?No
Replicate_Do_DB:
Replicate_Ignore_DB:
......
解決辦法一、
Slave_SQL_Running: No
1.程序可能在slave上進行了寫操作
2.也可能是slave機器重起后,事務回滾造成的.
一般是事務回滾造成的:
解決辦法:
mysql> stop?slave ;
mysql> set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
mysql> start?slave ;
解決辦法二、
首先停掉Slave服務:slave stop
到主服務器上查看主機狀態:
記錄File和Position對應的值
進入master
mysql> show master status;
+----------------------+----------+--------------+------------------+
| File?????????????????| Position | Binlog_Do_DB | Binlog_Ignore_DB |
+----------------------+----------+--------------+------------------+
| localhost-bin.000094 | 33622483 |??????????????|??????????????????|
+----------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
然后到slave服務器上執行手動同步:
mysql> change master to
> master_host='master_ip',
> master_user='user',
> master_password='pwd',
> master_port=3306,
> master_log_file=localhost-bin.000094',
> master_log_pos=33622483?;
1 row in set (0.00 sec)
mysql> start?slave ;
1 row in set (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
........
Master_Log_File: localhost-bin.000094
Read_Master_Log_Pos: 33768775
Relay_Log_File: localhost-relay-bin.000537
Relay_Log_Pos: 1094034
Relay_Master_Log_File: localhost-bin.000094
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
手動同步需要停止master的寫操作!
總結
以上是生活随笔為你收集整理的MariaDB(MySQL)_MariaDB(Mysql)-主从搭建的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 残缺棋盘问题算法分析_javascrip
- 下一篇: mysql sql诊断建议_MySQL诊