MariaDB下载安装(主从节点一样)
删除旧有数据库(注意自带MySQL也需要删除)
yum下载安装 yum -y install mariadb mariadb-server
建立数据存储路径
cd /opt && mkdir mysql
迁移数据存储路径到/opt下面
cp -r /var/lib/mysql/* /opt/mysql/
修改配置文件
vim /etc/my.cnf.d/mariadb-server.cnf
{{image.png}}
标红位置为重点配置
授权mysql 用户
chown -R mysql:mysql /opt/mysql/
启动mariadb
systemctl start mariadb
加入开机自启
systemctl enable mariadb
登录mysql初始化
UPDATE mysql.user SET password = PASSWORD('maysqlpassword') WHERE USER = 'root';
FLUSH PRIVILEGES;
加入开机自启
systemctl enable mariadb
MariaDB主从节点搭建
主节点配置文件(配置完需要重启服务)
#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#
# this is read by the standalone daemon and embedded servers
[server]
# this is only for the mysqld standalone daemon
# 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 mysqld/mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld]
datadir=/opt/mysql
socket=/opt/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#
# this is read by the standalone daemon and embedded servers
[server]
# this is only for the mysqld standalone daemon
# 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 mysqld/mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld]
datadir=/opt/mysql
socket=/opt/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
# 服务id 好跟内网IP后保持一致
server-id=186
# 开启二进制文件地址
log-bin=/opt/mysql/maste-log
# 同步二进制日志文件
sync_binlog = 1
# 由replication机制的SQL线程读取relay-log而执行的SQL语句记录到bin-log
log-slave-updates=true
[client]
port=3306
socket=/opt/mysql/mysql.sock
复制代码
登录MySQL 配置同步用户及 查看主节点信息
CREATE USER 'slave'@'%' IDENTIFIED BY 'maysqlpassword';
***这个权限很重要REPLICATION ***
GRANT REPLICATION SLAVE ON . TO 'slave'@'%';
刷新权限
flush privileges;
重置主键点信息
reset master;
查看主节点信息(查看完成后好锁表,数据变化会引起状态变化)
show master status \G;
{{image.png}}
红框信息需记住,从库配置需要用
从节点配置文件(配置完需要重启服务)
#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#
# this is read by the standalone daemon and embedded servers
[server]
# this is only for the mysqld standalone daemon
# 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 mysqld/mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld]
datadir=/opt/mysql
socket=/opt/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
server-id=187
# 开启二进制文件
log-bin=mysql-bin
log-slave-updates=true
[client]
port=3306
socket=/opt/mysql/mysql.sock
#
# * Galera-related settings
#
[galera]
# Mandatory settings
#wsrep_on=ON
#wsrep_provider=
#wsrep_cluster_address=
#binlog_format=row
#default_storage_engine=InnoDB
#innodb_autoinc_lock_mode=2
#
# Allow server to accept connections on all interfaces.
#
#bind-address=0.0.0.0
#
# Optional setting
#wsrep_slave_threads=1
#innodb_flush_log_at_trx_commit=0
# this is only for embedded server
[embedded]
复制代码
登录MySQL
停置从节点
stop slave;
重置从节点
reset slave all;
设置从节点信息
CHANGE MASTER TO MASTER_HOST='***.40.64.***',
MASTER_USER='slave', ---主节点设置得用户名
MASTER_PASSWORD='maysqlpassword', ---主节点设置得用户名密码
MASTER_LOG_FILE='maste-log.000002', ---主节点master设置得master file
MASTER_LOG_POS=313; ---主节点master设置得MASTER_LOG_POS
复制代码
查看从节点信息
``` show slave status \G; ```
复制代码
开启从节点服务
start slave;
再次查看从节点信息,
#### 这俩参数为yes 及配置成功
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
作者:sword
链接:https://juejin.cn/post/6982797483407573029