赞
踩
将172.30.11.12主机名修改为mysql1;172.30.11.13主机名修改为mysql2,命令如下:
mysql1节点:
[root@localhost ~]# hostnamectl set-hostname mysql1 [root@localhost ~]# logout [root@mysql1 ~]# hostnamectl Static hostname: mysql1 Icon name: computer-vm Chassis: vm Machine ID: 622ba110a69e24eda2dca57e4d306baa Boot ID: 859720a14f8f4b5e836f5a0fae7097e0 Virtualization: kvm Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-862.2.3.el7.x86_64 Architecture: x86-64
mysql2节点:
[root@localhost ~]# hostnamectl set-hostname mysql2 [root@localhost ~]# logout [root@mysql2 ~]# hostnamectl Static hostname: mysql2 Icon name: computer-vm Chassis: vm Machine ID: 622ba110a69e24eda2dca57e4d306baa Boot ID: 5e41c48c85704016ad0bd940500cc255 Virtualization: kvm Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-862.2.3.el7.x86_64 Architecture: x86-64
两个节点关闭防火墙firewalld及SELinux服务,命令如下:
# setenforce 0 # systemctl stop firewalld
两个节点配置/etc/hosts文件,修改为如下:
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 172.30.11.12 mysql1 172.30.11.13 mysql2
两个节点均使用提供的mariadb–10.3.23-repo.tar.gz的压缩包,解压并放在/opt目录下,进入/etc/yum.repos.d目录下,将原来的repo文件移除,新建local.repo文件并编辑内容,具体操作命令如下:
# curl -O http://10.24.1.82/training/mariadb-10.3.23-repo.tar.gz # curl -O http://10.24.1.82/training/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz # tar -zxvf mariadb-10.3.23-repo.tar.gz -C /opt/ # cd /etc/yum.repos.d/ # mv * /media/ # vi local.repo # cat local.repo [mariadb] name=mariadb baseurl=file:///opt/ gpgcheck=0 enabled=1
查看配置的YUM源是否可用,命令如下:
# yum repolist Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile repo id repo name status mariadb mariadb 19 repolist: 19
查看到repolist数量,即YUM源配置成功。
配置完毕后,两个节点安装数据库服务,命令如下:
# yum install -y mariadb mariadb-server
两个节点启动数据库服务并设置开机自启,命令如下:
# systemctl start mariadb # systemctl enable mariadb Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
两个节点初始化数据库,配置数据库root密码为000000,命令如下:
# mysql_secure_installation /usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found 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): #默认按Enter键 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密码000000 Re-enter new password: #再次输入密码000000 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!
修改mysql1节点的数据库配置文件,在配置文件/etc/my.cnf.d/server.cnf中的[mysqld]增添如下内容。
[root@mysql1 ~]# cat /etc/my.cnf.d/server.cnf ... ... [mysqld] log_bin = mysql-bin #记录操作日志 binlog_ignore_db = mysql #不同步MySQL系统数据库 server_id = 12 #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字,例如172.30.11.12,server_id就写12 ... ...
重启数据库服务,并进入数据库,命令如下:
[root@mysql1 ~]# systemctl restart mariadb [root@mysql1 ~]# mysql -uroot -p000000 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 9 Server version: 10.3.23-MariaDB-log 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)]>
在mysql1节点,授权在任何客户端机器上可以以root用户登录到数据库,然后在主节点上创建一个user用户连接节点mysql2,并赋予从节点同步主节点数据库的权限。命令如下:
MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by "000000"; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> grant replication slave on *.* to 'user'@'mysql2' identified by '000000'; Query OK, 0 rows affected (0.00 sec)
修改mysql2节点的数据库配置文件,在配置文件/etc/my.cnf.d/server.cnf中的[mysqld]增添如下内容。
[root@mysql2 ~]# cat /etc/my.cnf.d/server.cnf ... ... [mysqld] log_bin = mysql-bin #记录操作日志 binlog_ignore_db = mysql #不同步MySQL系统数据库 server_id = 13 #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字,例如172.30.11.13,server_id就写13 ... ...
修改完配置文件后,重启数据库服务,并在从节点mysql2上登录MariaDB数据库,配置从节点连接主节点的连接信息。master_host为主节点主机名mysql1,master_user为上一步中创建的用户user,命令如下:
[root@mysql2 ~]# systemctl restart mariadb [root@mysql2 ~]# mysql -uroot -p000000 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 9 Server version: 10.3.23-MariaDB-log 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)]> change master to master_host='mysql1',master_user='user',master_password='000000'; Query OK, 0 rows affected (0.01 sec)
配置完毕主从数据库之间的连接信息之后,开启从节点服务。使用show slave status\G命令,并查看从节点服务状态,如果Slave_IO_Running和Slave_SQL_Running的状态都为YES,则从节点服务开启成功。命令如下:
MariaDB [(none)]> start slave; MariaDB [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: mysql1 Master_User: user Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 705 Relay_Log_File: mysql2-relay-bin.000002 Relay_Log_Pos: 1004 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: 705 Relay_Log_Space: 1314 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: 12 Master_SSL_Crl: Master_SSL_Crlpath: Using_Gtid: No Gtid_IO_Pos: Replicate_Do_Domain_Ids: Replicate_Ignore_Domain_Ids: Parallel_Mode: conservative SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it Slave_DDL_Groups: 2 Slave_Non_Transactional_Groups: 0 Slave_Transactional_Groups: 0 1 row in set (0.000 sec)
可以看到Slave_IO_Running和Slave_SQL_Running的状态都是Yes,配置数据库主从集群成功。
先在主节点mysql1中创建库test,并在库test中创建表company,插入表数据,创建完成后,查看表company数据,命令如下:
[root@mysql1 ~]# mysql -uroot -p000000 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 11 Server version: 10.3.23-MariaDB-log 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 test; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> use test; Database changed MariaDB [test]> create table company(id int not null primary key,name varchar(50),addr varchar(255)); Query OK, 0 rows affected (0.01 sec) MariaDB [test]> insert into company values(1,"alibaba","china"); Query OK, 1 row affected (0.01 sec) MariaDB [test]> select * from company; +----+---------+-------+ | id | name | addr | +----+---------+-------+ | 1 | alibaba | china | +----+---------+-------+ 1 row in set (0.00 sec)
登录mysql2节点的数据库,查看数据库列表。找到test数据库,查询表,并查询内容验证从数据库的复制功能,命令如下:
[root@mysql2 ~]# mysql -uroot -p000000 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 12 Server version: 10.3.23-MariaDB-log 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 | | test | +--------------------+ 4 rows in set (0.00 sec) MariaDB [(none)]> use 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 | alibaba | china | +----+---------+-------+ 1 row in set (0.00 sec)
可以查看到主数据库中刚刚创建的库、表、信息,验证从数据库的复制功能成功。
将172.30.11.14的主机名修改为mycat,命令如下:
# hostnamectl set-hostname mycat # logout [root@mycat ~]# hostnamectl Static hostname: mycat Icon name: computer-vm Chassis: vm Machine ID: 622ba110a69e24eda2dca57e4d306baa Boot ID: c9e345d94abb4f0684ce75726c39dbcf Virtualization: kvm Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-862.2.3.el7.x86_64 Architecture: x86-64
在mycat节点安装Java JDK环境,具体操作步骤如下:
使用提供的mariadb-10.3.23-repo.tar.gz包上传至mycat节点的/root目录下,解压并配置成本地yum源,命令如下:
[root@mycat ~]# curl -O http://10.24.1.82/training/mariadb-10.3.23-repo.tar.gz [root@mycat ~]# tar -zxvf mariadb-10.3.23-repo.tar.gz -C /opt [root@mycat ~]# mv /etc/yum.repos.d/* /media/ [root@mycat ~]# vi /etc/yum.repos.d/local.repo [root@mycat ~]# cat /etc/yum.repos.d/local.repo [mariadb] name=mariadb baseurl=file:///opt/ gpgcheck=0 enabled=1
配置完yum源之后,进行安装Java JDK环境,命令如下:
[root@mycat ~]# yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel ... ...
安装完之后,可以使用命令查看Java JDK环境,命令如下:
[root@mycat ~]# java -version openjdk version "1.8.0_262" OpenJDK Runtime Environment (build 1.8.0_262-b10) OpenJDK 64-Bit Server VM (build 25.262-b10, mixed mode)
将Mycat服务的二进制软件包Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz上传到Mycat虚拟机的/root目录下,并将软件包解压到/use/local目录中。赋予解压后的Mycat目录权限。
[root@mycat ~]# curl -O http://10.24.1.82/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz [root@mycat ~]# tar -zxvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz -C /usr/local/ [root@mycat ~]# chmod -R 777 /usr/local/mycat/
配置Mycat服务读写分离的schema.xml配置文件在/usr/local/mycat/conf/目录下,可以在文件中定义一个逻辑库,使用户可以通过Mycat服务管理该逻辑库对应的MariaDB数据库。在这里定义一个逻辑库schema,name为USERDB;该逻辑库USERDB对应数据库database为test(在部署主从数据库时已创建);设置数据库写入节点为主节点mysql1;设置数据库读取节点为从节点mysql2。(可以直接删除原来schema.xml的内容,替换为如下。)
注意:IP需要修改成实际的IP地址。
[root@mycat ~]# cat /usr/local/mycat/conf/schema.xml <?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> <schema name="USERDB" checkSQLschema="true" sqlMaxLimit="100" dataNode="dn1"></schema> <dataNode name="dn1" dataHost="localhost1" database="test" /> <dataHost name="localhost1" maxCon="1000" minCon="10" balance="3" dbType="mysql" dbDriver="native" writeType="0" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <writeHost host="hostM1" url="172.30.11.12:3306" user="root" password="000000"> <readHost host="hostS1" url="172.30.11.13:3306" user="root" password="000000" /> </writeHost> </dataHost> </mycat:schema>
代码说明:
sqlMaxLimit:配置默认查询数量。
database:为真实数据库名。
balance=“0”:不开启读写分离机制,所有读操作都发送到当前可用的writeHost上。
balance=“1”:全部的readHost与stand by writeHost参与select语句的负载均衡,简单来说,当双主双从模式(M1->S1,M2->S2,并且M1与M2互为主备),正常情况下,M2、S1、S2都参与select语句的负载均衡。
balance=“2”:所有读操作都随机的在writeHost、readhost上分发。
balance=“3”:所有读请求随机地分发到wiriterHost对应的readhost执行,writerHost不负担读压力,注意balance=3只在1.4及其以后版本有,1.3版本没有。
writeType=“0”:所有写操作发送到配置的第一个writeHost,第一个挂了需要切换到还生存的第二个writeHost,重新启动后已切换后的为准,切换记录在配置文件dnindex.properties中。
writeType=“1”:所有写操作都随机的发送到配置的writeHost。
修改schema.xml的用户权限,命令如下:
[root@mycat ~]# chown root:root /usr/local/mycat/conf/schema.xml
修改/usr/local/mycat/conf/目录下的server.xml文件,修改root用户的访问密码与数据库,密码设置为000000,访问Mycat的逻辑库为USERDB,命令如下。
[root@mycat ~]# cat /usr/local/mycat/conf/server.xml
在配置文件的最后部分,
<user name="root"> <property name="password">000000</property> <property name="schemas">USERDB</property>
然后删除如下几行:
<user name="user"> <property name="password">user</property> <property name="schemas">TESTDB</property> <property name="readOnly">true</property> </user>
保存并退出server.xml配置文件。
通过命令启动Mycat数据库中间件服务,启动后使用netstat -ntpl命令查看虚拟机端口开放情况,如果有开放8066和9066端口,则表示Mycat服务开启成功。端口查询情况如图2-1所示。
[root@mycat ~]# /bin/bash /usr/local/mycat/bin/mycat start
先在Mycat虚拟机上使用Yum安装mariadb-client服务。
[root@mycat ~]# yum install -y MariaDB-client
在Mycat虚拟机上使用mysql命令查看Mycat服务的逻辑库USERDB,因为Mycat的逻辑库USERDB对应数据库test(在部署主从数据库时已安装),所以可以查看库中已经创建的表company。命令如下。
[root@mycat ~]# mysql -h127.0.0.1 -P8066 -uroot -p000000 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.6.29-mycat-1.6-RELEASE-20161028204710 MyCat Server (OpenCloundDB) Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]> show databases; +----------+ | DATABASE | +----------+ | USERDB | +----------+ 1 row in set (0.001 sec) MySQL [(none)]> use USERDB 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 MySQL [USERDB]> show tables; +----------------+ | Tables_in_test | +----------------+ | company | +----------------+ 1 row in set (0.003 sec) MySQL [USERDB]> select * from company; +----+---------+-------+ | id | name | addr | +----+---------+-------+ | 1 | alibaba | china | +----+---------+-------+ 1 row in set (0.005 sec)
在Mycat虚拟机上使用mysql命令对表company添加一条数据(2,“basketball”,“usa”),添加完毕后查看表信息。命令如下。
MySQL [USERDB]> insert into company values(2,"bastetball","usa"); Query OK, 1 row affected (0.050 sec) MySQL [USERDB]> select * from company; +----+------------+-------+ | id | name | addr | +----+------------+-------+ | 1 | alibaba | china | | 2 | bastetball | usa | +----+------------+-------+ 2 rows in set (0.003 sec)
在Mycat虚拟机节点使用mysql命令,通过9066端口查询对数据库读写操作的分离信息。可以看到所有的写入操作WRITE_LOAD数都在mysql1主数据库节点上,所有的读取操作READ_LOAD数都在mysql2主数据库节点上。由此可见,数据库读写操作已经分离到mysql1和mysql2节点上了。命令如下。
[root@mycat ~]# mysql -h127.0.0.1 -P9066 -uroot -p000000 -e 'show @@datasource;'
至此,Mycat读写分离数据库案例完成。
使用提供的OpenStack私有云平台,创建两台云主机vm1和vm2,镜像使用CentOS7.9,使用提供的mariadb-repo.tar.gz软件包,在这两台云主机上分别安装数据库服务,并配置成主从数据库,vm1节点为主库,vm2节点为从库(数据库密码设置为000000)。
1、下载 yum install -y mariadb-server mysqladmin -uroot password 123456 2、授权 mysql -uroot -p123456 -e "grant all on *.* to root@'%' identified by '123456'" 3、修改配置文件 log-bin=mysql-bin server_id=短ip 4、重启服务 systemctl restart mariadb 5、从节点执行 change master to master_user='root',master_host='192.168.100.160',master_password='123456'; 6、检测命令 mysql -uroot -p000000 -e "show slave status\G" Slave_IO_Running: Yes||Slave_SQL_Running: Yes
使用OpenStack私有云平台,创建3台系统为centos7.9的云主机,三台云主机进行安装高可用数据库集群(MariaDB_Galera_cluster,数据库密码设置为123456)的操作(所需的安装包在HTTP服务中)。
1、下载 yum install -y mariadb-server mysqladmin -uroot password 123456 2、授权 mysql -uroot -p123456 -e "grant all on *.* to root@'%' identified by '123456'" 3、修改配置文件 wsrep_on=ON wsrep_provider=/usr/lib64/galera/libgalera_smm.so wsrep_cluster_address="gcomm://vm1,vm2,vm3" #添加集群ip binlog_format=row default_storage_engine=InnoDB innodb_autoinc_lock_mode=2 bind-address=0.0.0.0 #取消注释 4、两个从节点stop mariadb 主节点启动 galera_new_cluster集群 然后从节点启动数据库服务。 5、检测命令 ansible node1 -a "netstat -ntpl" 0 0.0.0.0:4567 ansible node1 -m shell -a "mysql -uroot -p123456 -e \"show status like 'wsrep_ready';\" " wsrep_ready ON ansible node1 -m shell -a "mysql -uroot -p123456 -e \"show status like 'wsrep_cluster_size';\"" wsrep_cluster_size 3
yum install -y haproxy listen stats mode http bind vm1:9000 stats uri / backend mariadb balance roundrobin mode tcp bind vm1:3307 server vm1 vm1:3306 check weight 3 server vm2 vm2:3306 check weight 3 server vm3 vm3:3306 check weight 4 检测: 3307端口,weight
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。