码迷,mamicode.com
首页 > 数据库 > 详细

Linux与云计算——第二阶段Linux服务器架设 第一十二章:数据库搭建—MariaDB

时间:2016-08-13 14:21:50      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:安装mariadb   安装phpmyadmin   replication设置   galera集群   

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]
通过浏览器访问并登陆系统

 技术分享


[5]
已经登陆. You can operate MariaDB on here.

 技术分享

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/

root@repl01.example.com‘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 <info@codership.com>    |

| wsrep_provider_version                   | 25.3.9(r3387)                        |

| wsrep_ready                                       | ON                                   |

| wsrep_thread_count                         | 2                                    |

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

57 ows in set (0.00 sec)


本文出自 “11830455” 博客,请务必保留此出处http://11840455.blog.51cto.com/11830455/1837505

Linux与云计算——第二阶段Linux服务器架设 第一十二章:数据库搭建—MariaDB

标签:安装mariadb   安装phpmyadmin   replication设置   galera集群   

原文地址:http://11840455.blog.51cto.com/11830455/1837505

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!