热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

CentOS7MariaDB搭建主从服务器

2019独角兽企业重金招聘Python工程师标准本文编写环境为CentOS7。请先关闭SELinux,关闭防火墙或者防打开指定端口。本文测试环境部分信息如下#m

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

本文编写环境为CentOS7。

请先关闭SELinux,关闭防火墙或者防打开指定端口。本文测试环境部分信息如下

#master
[root@promote ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@promote ~]#
[root@promote ~]# ifconfig
ens33: flags=4163 mtu 1500inet 192.168.216.135 netmask 255.255.255.0 broadcast 192.168.216.255inet6 fe80::ccc2:d1b:1fc4:8ce2 prefixlen 64 scopeid 0x20ether 00:0c:29:7e:c3:24 txqueuelen 1000 (Ethernet)RX packets 913 bytes 70384 (68.7 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 438 bytes 47706 (46.5 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536inet 127.0.0.1 netmask 255.0.0.0inet6 ::1 prefixlen 128 scopeid 0x10loop txqueuelen 1000 (Local Loopback)RX packets 78 bytes 3900 (3.8 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 78 bytes 3900 (3.8 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0#slave
[root@promote ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@promote ~]#
[root@promote ~]# ifconfig
ens33: flags=4163 mtu 1500inet 192.168.216.136 netmask 255.255.255.0 broadcast 192.168.216.255inet6 fe80::9a13:471b:dee2:4e27 prefixlen 64 scopeid 0x20inet6 fe80::ccc2:d1b:1fc4:8ce2 prefixlen 64 scopeid 0x20inet6 fe80::c354:a1e1:869f:7ae1 prefixlen 64 scopeid 0x20ether 00:0c:29:36:5d:96 txqueuelen 1000 (Ethernet)RX packets 66317 bytes 89492193 (85.3 MiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 22069 bytes 1979972 (1.8 MiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536inet 127.0.0.1 netmask 255.0.0.0inet6 ::1 prefixlen 128 scopeid 0x10loop txqueuelen 1000 (Local Loopback)RX packets 0 bytes 0 (0.0 B)RX errors 0 dropped 0 overruns 0 frame 0TX packets 0 bytes 0 (0.0 B)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0[root@promote ~]#

为保证测试顺利进行,请关闭SELinux。

下文将演示主从服务器安装MariaDB Server,启动数据库,添加开机启动。

#安装软件
[root@promote ~]# yum install mariadb mariadb-devel mariadb-server -y
#启动服务
[root@promote ~]# systemctl start mariadb
查看服务运行状态
[root@promote ~]# systemctl status mariadb
● mariadb.service - MariaDB database serverLoaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)Active: active (running) since 一 2019-03-25 22:08:41 CST; 4s agoProcess: 7647 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)Process: 7616 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)Main PID: 7646 (mysqld_safe)CGroup: /system.slice/mariadb.service├─7646 /bin/sh /usr/bin/mysqld_safe --basedir=/usr└─7808 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --p...3月 25 22:08:38 promote.cache-dns.local systemd[1]: Starting MariaDB database server...
3月 25 22:08:38 promote.cache-dns.local mariadb-prepare-db-dir[7616]: Database MariaDB is probably initialized in /var/lib/mysql already, nothing is done.
3月 25 22:08:38 promote.cache-dns.local mysqld_safe[7646]: 190325 22:08:38 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
3月 25 22:08:38 promote.cache-dns.local mysqld_safe[7646]: 190325 22:08:38 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
3月 25 22:08:41 promote.cache-dns.local systemd[1]: Started MariaDB database server.
#添加开机启动项
[root@promote ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@promote ~]#

修改master 配置文件。vim /etc/my.cnf 。server_id=1 (不能相同)。主从服务器修改完毕配置文件并保存后重启数据库服务。

//配置文件增加内容
//server_id 不能相同
server_id=1 //[必须]服务器唯一ID,默认是1,一般取IP最后一段
log-bin=mysql-bin //[必须]启用二进制日志#master 配置文件
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
server_id=1
log-bin=mysql-bin
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid#
# include all files from the config directory
#
!includedir /etc/my.cnf.d#slave 配置文件
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
server_id=10
log-bin=mysql-bin
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

master服务器执行命令如下。

#登录数据库
[root@promote ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 222
Server version: 5.5.60-MariaDB MariaDB ServerCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.#允许root用户任意登录
MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* to 'root'@'%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)#查看master 状态
MariaDB [(none)]>
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 387 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)MariaDB [(none)]>

slave服务器执行命令并查看状态。

#登录slave服务器
[root@promote ~]# 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.60-MariaDB MariaDB ServerCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
#配置master信息,查看上文master_log_file和master_log_pos
#master_log_file='mysql-bin.000001',master_log_pos=387;
MariaDB [(none)]> change master to master_host='192.168.216.135',master_user='root',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=387;
Query OK, 0 rows affected (0.01 sec)MariaDB [(none)]> slave start;
Query OK, 0 rows affected (0.00 sec)
#部分选项注释
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************Slave_IO_State: Connecting to master #IO连接线程状态Master_Host: 192.168.216.135 #master 服务器地址Master_User: root #master用户名称Master_Port: 3306 #监听master端口信息Connect_Retry: 60 #连接失败重试连接时间Master_Log_File: mysql-bin.000001 #master服务器二进制日志文件名Read_Master_Log_Pos: 387 #master Position idRelay_Log_File: mariadb-relay-bin.000001 #slave 读取执行中继文件位置Relay_Log_Pos: 4 #slave 读取执行中继文件位置Relay_Master_Log_File: mysql-bin.000001 #Slave_IO_Running: Connecting #IO线程是否连接到master服务器状态【重要选项】Slave_SQL_Running: Yes #SQL线程启动情况【重要选项】Replicate_Do_DB: #同步数据库名称Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 387Relay_Log_Space: 245Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 2003Last_IO_Error: error connecting to master 'root@192.168.216.135:3306' - retry-time: 60 retries: 86400 message: Can't connect to MySQL server on '192.168.216.135' (113)Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 0
1 row in set (0.00 sec)MariaDB [(none)]>

检查Slave_IO_Running 和 Slave_SQL_Running状态均为Yes即可。

查询server_id结果。

#master server_id
MariaDB [db]> SHOW VARIABLES LIKE 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 1 |
+---------------+-------+
1 row in set (0.00 sec)MariaDB [db]> #slave server_idMariaDB [db]> SHOW VARIABLES LIKE 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 10 |
+---------------+-------+
1 row in set (0.01 sec)MariaDB [db]>

master创建一个简单数据库和表,并插入若干条数据,slave服务器查询数据库和表。

#master数据库操作[root@promote ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.60-MariaDB MariaDB ServerCopyright (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 master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 | 245 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)MariaDB [(none)]> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 1 |
+---------------+-------+
1 row in set (0.00 sec)MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)MariaDB [(none)]> create database db;
Query OK, 1 row affected (0.00 sec)MariaDB [(none)]> use db
Database changed
MariaDB [db]> create table test(id int,name text);
Query OK, 0 rows affected (0.00 sec)MariaDB [db]> insert into test values(1,"bill"),(2,"tom");
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0MariaDB [db]> #slave数据库操作[root@promote ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.60-MariaDB MariaDB ServerCopyright (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='192.168.216.135',master_user='root',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=245;
Query OK, 0 rows affected (0.00 sec)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 eventMaster_Host: 192.168.216.135Master_User: rootMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000003Read_Master_Log_Pos: 245Relay_Log_File: mariadb-relay-bin.000002Relay_Log_Pos: 529Relay_Master_Log_File: mysql-bin.000003Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 245Relay_Log_Space: 825Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1
1 row in set (0.00 sec)MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)MariaDB [(none)]> use db
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
MariaDB [db]> show tables;
+--------------+
| Tables_in_db |
+--------------+
| test |
+--------------+
1 row in set (0.00 sec)MariaDB [db]> select * from test;
+------+------+
| id | name |
+------+------+
| 1 | bill |
| 2 | tom |
+------+------+
2 rows in set (0.00 sec)MariaDB [db]>

附加内容:查找mariadb软件安装情况。

#方法1
[root@promote ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
mariadb-5.5.60-1.el7_5.x86_64
mariadb-server-5.5.60-1.el7_5.x86_64
mariadb-devel-5.5.60-1.el7_5.x86_64
#方法2
[root@promote ~]# yum list mariadb
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile* epel: mirror2.totbb.net
已安装的软件包
mariadb.x86_64 1:5.5.60-1.el7_5 @base
#方法3 可以查找安装目录,配置文件等
[root@promote ~]# rpm -ql mariadb | more
/etc/my.cnf.d/client.cnf
/usr/bin/aria_chk
/usr/bin/aria_dump_log
/usr/bin/aria_ftdump
/usr/bin/aria_pack
/usr/bin/aria_read_log
/usr/bin/msql2mysql
/usr/bin/my_print_defaults
/usr/bin/mysql
/usr/bin/mysql_find_rows
/usr/bin/mysql_waitpid
/usr/bin/mysqlaccess
/usr/bin/mysqladmin
/usr/bin/mysqlbinlog
/usr/bin/mysqlcheck
/usr/bin/mysqldump
/usr/bin/mysqlimport
/usr/bin/mysqlshow
/usr/bin/mysqlslap
/usr/share/doc/mariadb-5.5.60
/usr/share/doc/mariadb-5.5.60/COPYING
/usr/share/doc/mariadb-5.5.60/COPYING.Google
/usr/share/doc/mariadb-5.5.60/COPYING.Percona
/usr/share/doc/mariadb-5.5.60/README
/usr/share/doc/mariadb-5.5.60/README.mysql-docs
/usr/share/doc/mariadb-5.5.60/README.mysql-license
/usr/share/man/man1/aria_chk.1.gz
/usr/share/man/man1/aria_dump_log.1.gz
/usr/share/man/man1/aria_ftdump.1.gz
/usr/share/man/man1/aria_pack.1.gz
/usr/share/man/man1/aria_read_log.1.gz
/usr/share/man/man1/my_print_defaults.1.gz
/usr/share/man/man1/mysql.1.gz
/usr/share/man/man1/mysql_find_rows.1.gz
/usr/share/man/man1/mysql_waitpid.1.gz
/usr/share/man/man1/mysqlaccess.1.gz
/usr/share/man/man1/mysqladmin.1.gz
/usr/share/man/man1/mysqldump.1.gz
/usr/share/man/man1/mysqlshow.1.gz
/usr/share/man/man1/mysqlslap.1.gz
[root@promote ~]#

 


转:https://my.oschina.net/u/1011130/blog/3027695



推荐阅读
  • Centos7.6安装Gitlab教程及注意事项
    本文介绍了在Centos7.6系统下安装Gitlab的详细教程,并提供了一些注意事项。教程包括查看系统版本、安装必要的软件包、配置防火墙等步骤。同时,还强调了使用阿里云服务器时的特殊配置需求,以及建议至少4GB的可用RAM来运行GitLab。 ... [详细]
  • 基于PgpoolII的PostgreSQL集群安装与配置教程
    本文介绍了基于PgpoolII的PostgreSQL集群的安装与配置教程。Pgpool-II是一个位于PostgreSQL服务器和PostgreSQL数据库客户端之间的中间件,提供了连接池、复制、负载均衡、缓存、看门狗、限制链接等功能,可以用于搭建高可用的PostgreSQL集群。文章详细介绍了通过yum安装Pgpool-II的步骤,并提供了相关的官方参考地址。 ... [详细]
  • Linux服务器密码过期策略、登录次数限制、私钥登录等配置方法
    本文介绍了在Linux服务器上进行密码过期策略、登录次数限制、私钥登录等配置的方法。通过修改配置文件中的参数,可以设置密码的有效期、最小间隔时间、最小长度,并在密码过期前进行提示。同时还介绍了如何进行公钥登录和修改默认账户用户名的操作。详细步骤和注意事项可参考本文内容。 ... [详细]
  • 本文介绍了在rhel5.5操作系统下搭建网关+LAMP+postfix+dhcp的步骤和配置方法。通过配置dhcp自动分配ip、实现外网访问公司网站、内网收发邮件、内网上网以及SNAT转换等功能。详细介绍了安装dhcp和配置相关文件的步骤,并提供了相关的命令和配置示例。 ... [详细]
  • 本文介绍了数据库的存储结构及其重要性,强调了关系数据库范例中将逻辑存储与物理存储分开的必要性。通过逻辑结构和物理结构的分离,可以实现对物理存储的重新组织和数据库的迁移,而应用程序不会察觉到任何更改。文章还展示了Oracle数据库的逻辑结构和物理结构,并介绍了表空间的概念和作用。 ... [详细]
  • 图解redis的持久化存储机制RDB和AOF的原理和优缺点
    本文通过图解的方式介绍了redis的持久化存储机制RDB和AOF的原理和优缺点。RDB是将redis内存中的数据保存为快照文件,恢复速度较快但不支持拉链式快照。AOF是将操作日志保存到磁盘,实时存储数据但恢复速度较慢。文章详细分析了两种机制的优缺点,帮助读者更好地理解redis的持久化存储策略。 ... [详细]
  • Linux如何安装Mongodb的详细步骤和注意事项
    本文介绍了Linux如何安装Mongodb的详细步骤和注意事项,同时介绍了Mongodb的特点和优势。Mongodb是一个开源的数据库,适用于各种规模的企业和各类应用程序。它具有灵活的数据模式和高性能的数据读写操作,能够提高企业的敏捷性和可扩展性。文章还提供了Mongodb的下载安装包地址。 ... [详细]
  • 本文介绍了Windows操作系统的版本及其特点,包括Windows 7系统的6个版本:Starter、Home Basic、Home Premium、Professional、Enterprise、Ultimate。Windows操作系统是微软公司研发的一套操作系统,具有人机操作性优异、支持的应用软件较多、对硬件支持良好等优点。Windows 7 Starter是功能最少的版本,缺乏Aero特效功能,没有64位支持,最初设计不能同时运行三个以上应用程序。 ... [详细]
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • 本文介绍了在Linux下安装Perl的步骤,并提供了一个简单的Perl程序示例。同时,还展示了运行该程序的结果。 ... [详细]
  • 本文介绍了Linux系统中正则表达式的基础知识,包括正则表达式的简介、字符分类、普通字符和元字符的区别,以及在学习过程中需要注意的事项。同时提醒读者要注意正则表达式与通配符的区别,并给出了使用正则表达式时的一些建议。本文适合初学者了解Linux系统中的正则表达式,并提供了学习的参考资料。 ... [详细]
  • Ubuntu 9.04中安装谷歌Chromium浏览器及使用体验[图文]
    nsitionalENhttp:www.w3.orgTRxhtml1DTDxhtml1-transitional.dtd ... [详细]
  • 本文讨论了在数据库打开和关闭状态下,重新命名或移动数据文件和日志文件的情况。针对性能和维护原因,需要将数据库文件移动到不同的磁盘上或重新分配到新的磁盘上的情况,以及在操作系统级别移动或重命名数据文件但未在数据库层进行重命名导致报错的情况。通过三个方面进行讨论。 ... [详细]
  • 本文介绍了将mysql从5.6.15升级到5.7.15的详细步骤,包括关闭访问、备份旧库、备份权限、配置文件备份、关闭旧数据库、安装二进制、替换配置文件以及启动新数据库等操作。 ... [详细]
author-avatar
企鹅
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有