1. 程式人生 > 其它 >mysql主從資料庫

mysql主從資料庫

改主機名

[root@server ~]# hostnamectl set-hostname mysql1
[root@server ~]# bash
[root@mysql1 ~]#
[root@client ~]# hostnamectl set-hostname mysql2
[root@client ~]# bash
[root@mysql2 ~]#

配置網路和yum源此處省略

防火牆

如果資料庫訪問不成功,則需要關閉防火牆才可進行訪問;
命令為: systemctl stop firewalld  (關閉防火牆並不安全)
檢視防火牆狀態命令: systemctl status firewalld
開啟防火牆命令為: systemctl start firewalld
如果不想關閉防火牆服務,可使用firewall-cmd命令新增規則;

寫域名解析檔案

[root@mysql1 ~]# vi /etc/hosts
末行寫入
192.168.100.10		mysql1
192.168.100.20		mysql2
儲存退出
[root@mysql1 ~]# scp /etc/hosts 192.168.100.20:/etc  #複製一份到MySQL2或者去mysql2自己編輯寫入

下載mysql服務

[root@mysql1 ~]# yum install -y mariadb mariadb-server
[root@mysql2 ~]# yum install -y mariadb mariadb-server

重啟並設定自啟動

-兩邊都要重啟(此處只寫一邊)
[root@mysql1 ~]# systemctl start mariadb
[root@mysql1 ~]# systemctl enable mariadb

安全配置嚮導

-兩邊都要配置(此處只寫一邊)
[root@mysql1 ~]# 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): 
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: 
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] n
 ... skipping.

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] 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!

總結來說就是:
回車 (下次進入需要輸入設定的密碼)
y
設定密碼
檢查密碼
y
n
y
y

編輯配置檔案

mysql1

[root@mysql1 ~]# vim /etc/my.cnf
末行寫入
[mysqld]
log-bin = mysql-bin
server-id =10  #(mysql1 ip:192.168.100.10,mysql2 ip:192.168.100.20)

mysql2

[root@mysql2 ~]# vim /etc/my.cnf
末行寫入
[mysqld]
server-id =20  #(mysql1 ip:192.168.100.10,mysql2 ip:192.168.100.20)

重啟

[root@mysql1 ~]# systemctl restart mariadb
[root@mysql2 ~]# systemctl restart mariadb

進行主從同步的二三四步

mysql1

[root@mysql1 ~]# mysql -uroot -p11111  #進入mysql資料庫 五個一為自己設定的密碼
MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by "11111";
#對所有遠端root身份登入並對所有庫和表賦予讀寫許可權,通過密碼為11111

MariaDB [(none)]> grant replication slave on *.* to 'user'@'mysql2' identified by '11111';    
  #此處mysql2需和域名解析主機名一致
#所有庫和所有表對從資料庫賦予複製許可權,並通過user使用者登入到mysql2,通過密碼為11111

mysql2

[root@mysql2 ~]# mysql -uroot -p11111  #進入mysql資料庫 五個一為自己設定的密碼
MariaDB [(none)]> grant all privileges  on *.* to root@'%' identified by "11111";
#同上
MariaDB [(none)]> change master to master_host='mysql1',master_user='user',master_password='11111';
    #此處mysql1需和域名解析主機名一致
#改變主人為主資料庫,使用者為user,主庫通過密碼為11111

MariaDB [(none)]> start slave;
#開始為“從庫”
MariaDB [(none)]> show slave status\G
#查詢從庫狀態 Slave_IO_Running: Yes
            Slave_SQL_Running: Yes 為yes即可

驗證主從同步的結果

mysql1

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
#原本就有的資料庫

MariaDB [(none)]> create database test;  #建立一個名為test的資料庫
MariaDB [(none)]> use test;  #使用test資料庫
MariaDB [(none)]> create table company(id int not null primary key,name varchar(50),addr varchar(255));
#建立一個名為company的表並設定欄位型別

MariaDB [test]> insert into company values(1,"facebook","usa");  #插入資料

mysql2

MariaDB [(none)]> show databases; #mysql2中檢視資料庫 可以看到test
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> use test;  #使用test資料庫 而且顯示只可讀
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [test]> show tables;  #查看錶
+----------------+
| Tables_in_test |
+----------------+
| company        |
+----------------+
1 row in set (0.00 sec)

MariaDB [test]> select * from company;  #顯示錶記錄
+----+----------+------+
| id | name     | addr |
+----+----------+------+
|  1 | facebook | usa  |
+----+----------+------+
1 row in set (0.00 sec)

從庫建立資料庫不會同步到主庫並且主庫看不到

mysql2

MariaDB [(none)]> create database zzz;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| zzz                |
+--------------------+
5 rows in set (0.00 sec)

mysql1

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

本部落格轉載於:https://www.cnblogs.com/zhengyan6/p/15685486.html