我的github
yum install -y mariadb-server
mysql_secure_installation #MySql初始化脚本,以下为每一项的翻译
是否设置root密码
输入密码
确认密码
是否设置匿名用户
是否允许root远程登录
删除test数据库
现在是否生效
添加PATH变量
systemctl start mariadb
hostnamectl set-hostname mysql-master
exec bash
MariaDB [(none)]> create database shuaiguoxia ;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| shuaiguoxia |
| test |
+--------------------+
5 rows in set (0.00 sec)
use shuaiguoxia;
create table blog (name varchar(20),age int,sex varchar(10));
MariaDB [shuaiguoxia]> desc blog;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| sex | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
insert into blog (name,age,sex) values ("guo",27,"nan");
insert into blog (name,age,sex) values ("wu",33,"nan");
insert into blog (name,age,sex) values ("cai",31,"nv");
insert into blog (name,age,sex) values ("li",19,"nan");
insert into blog (name,age,sex) values ("zhao",18,"nan");
insert into blog (name,age,sex) values ("qian",25,"nv");
vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
server-id=10
log-bin=mysql-bin
relay-log=mysql-relay-bin
replicate-wild-ignore-table=mysq.%
mysql -u root -p
use shuaiguoxia;
MariaDB [shuaiguoxia]> lock tables blog read;
Query OK, 0 rows affected (0.00 sec)
mysqldump -u root -p -h 127.0.0.1 --database shuaiguoxia > /bak.sql
Enter password:
# 导出时一定要--dabatase指定数据库
scp /bak.sql 192.168.1.175:/
mysql -u root -p < /bak.sql
grant replication slave on *.* to 'slave_user'@'192.168.1.%' identified by '1234'
hostnamectl set-hostname slave-master
exec bash
[root@mysql-slave ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
server-id=21
log-bin=mysql-bin
replicate-wild-ignore-table=mysql.%
# 查看主节点的master状态,要在主节点执行
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 | 2900 | | |
+------------------+----------+--------------+------------------+
# 前面导入过就不同导入了,怕你忘了再提一句
mysql -u root -p < /bak.sql
# 命令中的master_log_file和master_log_pos为主节点查询的结果
MariaDB [(none)]> change master to \
master_host='192.168.1.46',
master_user='slave_user',
master_password='1234',
master_log_file='mysql-bin.000003',
master_log_pos=2900;
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.01 sec)
# 正常状态下Slave_IO_Running和 Slave_SQL_Running都为yes
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.46
Master_User: slave_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 3057
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 686
Relay_Master_Log_File: mysql-bin.000003
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: mysql.%
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 3057
Relay_Log_Space: 982
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: 10
1 row in set (0.00 sec)
mysqldump -u root -p -h 127.0.0.1 --database shuaiguoxia > /bak.sql
Enter password:
# 导出时一定要--dabatase指定数据库
mysql -u root -p < /bak.sql