Learn More →
DB
$ sudo yum groupinstall mariadb mariadb-client -y
$ sudo yum install mariadb mariadb-server -y
$ sudo systemctl start mariadb
$ sudo systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
$ sudo systemctl status mariadb
● mariadb.service - MariaDB database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2019-09-10 10:05:12 CST; 4min 57s ago
Main PID: 2067 (mysqld_safe)
CGroup: /system.slice/mariadb.service
├─2067 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
└─2229 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
Sep 10 10:05:10 centos7-cli.lab.example.com systemd[1]: Starting MariaDB database server...
Sep 10 10:05:10 centos7-cli.lab.example.com mariadb-prepare-db-dir[2035]: Database MariaDB is probably initialized in /var/lib/mysql already, nothing is done.
Sep 10 10:05:10 centos7-cli.lab.example.com mariadb-prepare-db-dir[2035]: If this is not the case, make sure the /var/lib/mysql is empty before running mariadb-prepare-db-dir.
Sep 10 10:05:10 centos7-cli.lab.example.com mysqld_safe[2067]: 190910 10:05:10 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
Sep 10 10:05:10 centos7-cli.lab.example.com mysqld_safe[2067]: 190910 10:05:10 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
Sep 10 10:05:12 centos7-cli.lab.example.com systemd[1]: Started MariaDB database server.
$ sudo 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] 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] 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!
這段 script 主要在做以下動作:
$ mysql -V
mysql Ver 15.1 Distrib 5.5.60-MariaDB, for Linux (x86_64) using readline 5.1
$ mysql -u root -h localhost -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
MariaDB [(none)]> exit
Bye
[mysqld]
server_id=1
log-basename=master
log-bin
binlog-format=row
binlog-do-db=MasterA
mysqlbinlog /var/lib/mysql/mariadb-bin.000001
或是在 Mariadb 資料庫操作介面下指令
MariaDB [(none)]> show binlog events \G
binlog-do-db=MasterA
binlog-do-db=MasterB
沒設定時就會同步所有資料庫,或是使用下面忽略的方式來設定。
systemctl restart mariadb
[root@localhost ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.64-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> CREATE DATABASE MasterA;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'repliuser'@'%' IDENTIFIED BY '29057419';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000001 | 780 | MasterA | |
+-------------------+----------+--------------+------------------+
##Centos7 防火牆開啟埠號
firewall-cmd --zone=public --add-port=3306/tcp --permanent
#下面3行是引數說明
#–zone #作用域
#–add-port=80/tcp #新增埠,格式為:埠/通訊協議
#–permanent #永久生效,沒有此引數重啟後失效
#重啟防火牆後看看是否生效
firewall-cmd --reload #重啟firewall
firewall-cmd --list-ports #檢視已經開放的埠
#如果想永久停止防火牆,執行下面操作
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall開機啟動
#檢視防火牆狀態
firewall-cmd --state #檢視預設防火牆狀態(關閉後顯示notrunning,開啟後顯示running)
[mysqld]
skip_name_resolve = ON
server-id = 2
relay-log = slave-log
read_only = 1
report-port = 3306
report-host = 34.80.160.17
replicate-do-db = MasterA
replicate-do-db = MasterB
replicate_wild_do_table=MasterA.%
replicate_wild_do_table=MasterB.%
replicate-do-db = MasterA
replicate-do-db = MasterB
replicate_wild_do_table=MasterA.%
replicate_wild_do_table=MasterB.%
systemctl restart mariadb
mysql -u root -p
CHANGE MASTER 'MasterA' TO MASTER_HOST='59.124.115.145',
MASTER_USER='repliuser',
MASTER_PORT=3306,
MASTER_PASSWORD='29057419',
MASTER_LOG_FILE='master-bin.000001',
MASTER_LOG_POS=780;
START SLAVE 'MasterA';
MariaDB [(none)]> show slave 'MasterA' status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 59.124.115.145
Master_User: repliuser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mariadb-bin.000001
Read_Master_Log_Pos: 780
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 780
Relay_Master_Log_File: mariadb-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: MasterA,MasterB
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table: MasterA.%,MasterB.%
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 554
Relay_Log_Space: 827
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
1 row in set (0.00 sec)
[root@localhost ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 5.5.64-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> USE MasterA;
Database changed
MariaDB [MasterA]> CREATE TABLE test (ID INT);
Query OK, 0 rows affected (0.00 sec)
MariaDB [MasterA]> INSERT INTO test VALUES(1);
Query OK, 1 row affected (0.00 sec)
MariaDB [MasterA]> SELECT * FROM test;
+------+
| ID |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
MariaDB [(none)]> USE MasterA;
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 [MasterA]> SELECT * FROM test;
+------+
| ID |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
當上述的master slave 架構都成功後可以參考下一篇Maxscale的教學,嘗試實作DB的負載平衡 網址:https://hackmd.io/@WL-WTIRiRlOr-R2wORqerA/SJP8CjU1O