Linux與雲計算——第二階段Linux服務器架設 第一十二章:數據庫搭建—MariaDB

Linux與雲計算——第二階段Linux服務器架設

第一十二章:數據庫搭建—MariaDB


2.1 安裝MariaDB

[1] 安裝並啓動 MariaDB.

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

[root@server ~]# vim /etc/my.cnf

# [mysqld]部分添加

[mysqld]

character-set-server=utf8

[root@server ~]# systemctl start mariadb

[root@server ~]# systemctl enable mariadb

ln -s '/usr/lib/systemd/system/mariadb.service' '/etc/systemd/system/multi-user.target.wants/mariadb.service'

[2] MariaDB初始化設置.

[root@client ~]# mysql_secure_installation

使用root用戶連接

[root@client ~]# mysql -u root –p

Enter password:

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 3

Server version: 5.5.37-MariaDB MariaDB Server

Copyright (c) 2000, 2014, Oracle, Monty Program Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

# 查看用戶列表

MariaDB [(none)]> select user,host,password from mysql.user;

+------+-----------+-------------------------------------------+

| user | host      | password                                  |

+------+-----------+-------------------------------------------+

| root | localhost | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |

| root | 127.0.0.1 | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |

| root | ::1       | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |

+------+-----------+-------------------------------------------+

3 rows in set (0.00 sec)

# 查看數據庫列表

MariaDB [(none)]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

+--------------------+

3 rows in set (0.00 sec)

MariaDB [(none)]> exit

Bye

2.2 安裝phpMyAdmin

[1] 安裝並啓動Apache httpd

[2] 安裝PHP

[3] 安裝phpMyAdmin.

[root@server ~]# yum -y install phpMyAdmin php-mysql php-mcrypt

[root@server ~]# vi /etc/httpd/conf.d/phpMyAdmin.conf

# line 15: 你允許訪問的網段

Require ip 127.0.0.1 192.168.96.0/24

# line 32: 你允許訪問的網段

Require ip 127.0.0.1 192.168.96.0/24

[root@client ~]# systemctl restart httpd


[4]
通過瀏覽器訪問並登陸系統

 wKiom1eugCzA7ggHAACTgslYJrM652.png-wh_50


[5]
已經登陸. You can operate MariaDB on here.

 wKioL1eugDeQLuCxAAEFcBdy-tg984.png-wh_50

2.3 Replication設置

MariaDB  Replication的 主從配置的設置。

 [1]更改設置,並且創建一個用戶

 [root@client ~]# vi /etc/my.cnf

[mysqld]

# mysqld 部分添加以下部分,得到二進制日誌。

log-bin=mysql-bin

# 定義server ID

server-id=101

[root@client ~]# systemctl restart mariadb

[root@client ~]# 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.41-MariaDB-log MariaDB Server

Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

#創建用戶(set any password for 'password' section)

MariaDB [(none)]> grant replication slave on *.* to replica@'%' identified by 'password';

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> exit

Bye

[2] Install and start MariaDB Server on Slave Host, refer to here.

[3] Change settings on Slave Host.

[root@repl01 ~]# vi /etc/my.cnf

[mysqld]

# mysqld 部分添加以下部分,得到二進制日誌。

log-bin=mysql-bin

# 定義server ID,不同於主配置文件的id

server-id=102

# 只讀

read_only=1

# 定義自己的主機名

report-host=repl01.example.com

[root@repl01 ~]# systemctl restart mariadb

[4] Get Dump-Data on Master Host.

[root@client ~]# mysql -u root -p

Enter password:

Your MariaDB connection id is 3

Server version: 5.5.41-MariaDB-log MariaDB Server

Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

#鎖住所有的表

MariaDB [(none)]> flush tables with read lock;

Query OK, 0 rows affected (0.00 sec)

# 顯示狀態 (remember File, Position value)

MariaDB [(none)]> show master status;

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000001 |      465 |              |                  |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

#保留之前的窗口,打開一個新的執行轉儲

 [root@client ~]# mysqldump -u root -p --all-databases --lock-all-tables --events > mysql_dump.sql

Enter password:

# 返回之前的窗口,並解鎖

MariaDB [(none)]> unlock tables;

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> exit

Bye

#將轉儲轉發至從服務器

 [root@client ~]# scp mysql_dump.sql repl01.example.com:/tmp/

[email protected]'s password:

mysql_dump.sql 100% 515KB 514.7KB/s 00:00

[5]在從服務器上配置replication

準備好之後,確信工作正常從而在主服務器創建數據庫

#從主服務器導入轉儲

 [root@repl01 ~]# mysql -u root -p < /tmp/mysql_dump.sql

Enter password:

[root@repl01 ~]# mysql -u root -p

Enter password:

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 3

Server version: 5.5.41-MariaDB-log MariaDB Server

Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> change master to

    -> master_host='10.0.0.31',   # Master Hosts's IP

    -> master_user='replica',   # replication ID

    -> master_password='password',   # replication ID's password

    -> master_log_file='mysql-bin.000001',   # File value confirmed on Master

    -> master_log_pos=465;   # Position value confirmed on Master

Query OK, 0 rows affected (0.58 sec)

# 開始 replication

MariaDB [(none)]> start slave;

Query OK, 0 rows affected (0.00 sec)

# 顯示狀態

MariaDB [(none)]> show slave status\G

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 10.0.0.31

                  Master_User: replica

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000001

          Read_Master_Log_Pos: 536

               Relay_Log_File: mariadb-relay-bin.000002

                Relay_Log_Pos: 600

        Relay_Master_Log_File: mysql-bin.000001

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB:

          Replicate_Ignore_DB:

           Replicate_Do_Table:

       Replicate_Ignore_Table:

      Replicate_Wild_Do_Table:

  Replicate_Wild_Ignore_Table:

                   Last_Errno: 0

                   Last_Error:

                 Skip_Counter: 0

          Exec_Master_Log_Pos: 536

              Relay_Log_Space: 896

              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: 101

1 row in set (0.00 sec)

2.4 Galera集羣

Configure MariaDB Galera Cluster.

All nodes in cluster become Master-Server in this configuration.

[1] 在所有節點安裝MariaDB Galera

 [root@client ~]# vi /etc/yum.repos.d/mariadb.repo

 # MariaDB 10.0 CentOS repository list

# http://mariadb.org/mariadb/repositories/

[mariadb]

name = MariaDB

baseurl = http://yum.mariadb.org/10.0/centos7-amd64

gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB

gpgcheck=1

enabled=0

[root@client ~]# yum --enablerepo=mariadb -y install MariaDB-Galera-server

[2] Configure a 1st node like follows.

[root@client ~]# vi /etc/my.cnf.d/server.cnf

# line 19: uncomment and change like follows

[galera]

# Mandatory settings

wsrep_provider=/usr/lib64/galera/libgalera_smm.so

# specify all nodes in cluster

wsrep_cluster_address="gcomm://10.0.0.31,10.0.0.51"

# uncomment all

binlog_format=row

default_storage_engine=InnoDB

innodb_autoinc_lock_mode=2

bind-address=0.0.0.0

# 添加下面

# 定義集羣的名字

wsrep_cluster_name="MariaDB_Cluster"

# ip地址

wsrep_node_address="10.0.0.31"

# replication提供者

wsrep_sst_method=rsync

[root@client ~]# /etc/rc.d/init.d/mysql bootstrap

Bootstrapping the cluster.. Starting MySQL.. SUCCESS!

# 加載基本的信息

[root@client ~]# mysql_secure_installation

[3] Configure other nodes except a node of previous section like follows.

[root@node01 ~]# vi /etc/my.cnf.d/server.cnf

# line 19: 更改下面的信息

[galera]

# 強制設置

wsrep_provider=/usr/lib64/galera/libgalera_smm.so

# 指定集羣中的所有節點

wsrep_cluster_address="gcomm://10.0.0.31,10.0.0.51"

#取消

binlog_format=row

default_storage_engine=InnoDB

innodb_autoinc_lock_mode=2

bind-address=0.0.0.0

# 添加信息

# 集羣名稱

wsrep_cluster_name="MariaDB_Cluster"

# IP 地址

wsrep_node_address="10.0.0.51"

# replication 提供者

wsrep_sst_method=rsync

[root@node01 ~]# systemctl start mysql

[4] The Cluster settings is OK, Make sute the status like follows. It's OK if "wsrep_local_state_comment" is "Synced".

[root@node01 ~]# mysql -u root -p

Enter password:

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 6

Server version: 10.0.20-MariaDB-wsrep MariaDB Server, wsrep_25.10.r4144

 

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

MariaDB [(none)]> show status like 'wsrep_%';

+------------------------------+--------------------------------------+

| Variable_name                | Value                                |

+------------------------------+--------------------------------------+

| wsrep_local_state_uuid       | 20329169-3414-11e5-9285-cab5ed757f81 |

| wsrep_protocol_version        | 7                                    |

| wsrep_last_committed          | 3                                    |

| wsrep_replicated                    | 0                                    |

| wsrep_replicated_bytes        | 0                                    |

| wsrep_repl_keys                     | 0                                    |

| wsrep_repl_keys_bytes         | 0                                    |

| wsrep_repl_data_bytes         | 0                                    |

| wsrep_repl_other_bytes       | 0                                    |

| wsrep_received                       | 3                                    |

| wsrep_received_bytes           | 237                                  |

| wsrep_local_commits             | 0                                    |

| wsrep_local_cert_failures      | 0                                    |

| wsrep_local_replays                | 0                                    |

| wsrep_local_send_queue       | 0                                    |

| wsrep_local_send_queue_max   | 2                                    |

| wsrep_local_send_queue_min    | 0                                    |

| wsrep_local_send_queue_avg     | 0.333333                             |

| wsrep_local_recv_queue              | 0                                    |

| wsrep_local_recv_queue_max     | 1                                    |

| wsrep_local_recv_queue_min      | 0                                    |

| wsrep_local_recv_queue_avg       | 0.000000                             |

| wsrep_local_cached_downto        | 18446744073709551615                 |

| wsrep_flow_control_paused_ns   | 0                                    |

| wsrep_flow_control_paused          | 0.000000                             |

| wsrep_flow_control_sent               | 0                                    |

| wsrep_flow_control_recv               | 0                                    |

| wsrep_cert_deps_distance             | 0.000000                             |

| wsrep_apply_oooe                           | 0.000000                             |

| wsrep_apply_oool                            | 0.000000                             |

| wsrep_apply_window                      | 0.000000                             |

| wsrep_commit_oooe                       | 0.000000                             |

| wsrep_commit_oool                        | 0.000000                             |

| wsrep_commit_window                  | 0.000000                             |

| wsrep_local_state                             | 4                                    |

| wsrep_local_state_comment          | Synced                               |

| wsrep_cert_index_size                     | 0                                    |

| wsrep_causal_reads                          | 0                                    |

| wsrep_cert_interval                           | 0.000000                             |

| wsrep_incoming_addresses             | 10.0.0.31:3306,10.0.0.51:3306        |

| wsrep_evs_delayed                           |                                      |

| wsrep_evs_evict_list                          |                                      |

| wsrep_evs_repl_latency                    | 0/0/0/0/0                            |

| wsrep_evs_state                                 | OPERATIONAL                          |

| wsrep_gcomm_uuid                          | 69d7b95d-3415-11e5-9666-7be9d4b6159d |

| wsrep_cluster_conf_id                      | 2                                    |

| wsrep_cluster_size                            | 2                                    |

| wsrep_cluster_state_uuid                | 20329169-3414-11e5-9285-cab5ed757f81 |

| wsrep_cluster_status                        | Primary                              |

| wsrep_connected                             | ON                                   |

| wsrep_local_bf_aborts                    | 0                                    |

| wsrep_local_index                            | 1                                    |

| wsrep_provider_name                     | Galera                               |

| wsrep_provider_vendor                   | Codership Oy <[email protected]>    |

| wsrep_provider_version                   | 25.3.9(r3387)                        |

| wsrep_ready                                       | ON                                   |

| wsrep_thread_count                         | 2                                    |

+------------------------------+--------------------------------------+

57 ows in set (0.00 sec)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章