#MariaDB数据库安装并同步
##具体步骤
1.安装一个数据库(MariaBD
)
2.有一个xxx.sql.gz
数据库备份文件
3.导入到新的数据库中
4.让它正常同步
###一、MariaDB的安装和配置
####1、添加yum数据源
建议命名为 MariaDB.repo 类似的名字:
cd /etc/yum.repos.d/
vim /etc/yum.repos.d/MariaDB.repo
然后,写入内容:
# MariaDB 10.0 CentOS repository list - created 2016-03-23 07:50 UTC
# http://mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.0/centos6-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
该文件的内容是参考官网,并从官网上生成的,设置安装源仓库的 具体的地址为: https://downloads.mariadb.org/mariadb/repositories/
选择好操作系统版本之后既可以查看,其他操作系统的安装源也可以在此处查看并设置。
####2、yum安装MariaDB
yum -y install MariaDB-server MariaDB-client
####3、启动数据库
# 查看mysql状态;关闭数据库
# service mysql status
# service mysql stop
# 启动数据库
service mysql start
####4、修改root密码
mysql_secure_installation
使用这个命令来修改密码有很多好处,拒绝远程登录,而且不会出现空密码,可以把空白的用户删除掉,还能指定域是在内网范围的。
MariaDB [mysql]> select user,password,host from user;
+------+-------------------------------------------+-----------------------+
| user | password | host |
+------+-------------------------------------------+-----------------------+
| root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | localhost |
| root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | localhost.localdomain |
| root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 127.0.0.1 |
| root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | ::1 |
| rep1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 192.168.42.% |
| rep | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 192.168.42.% |
+------+-------------------------------------------+-----------------------+
测试,如果使用mysqladmin来修改密码:
mysqladmin -u root password '123456'
MariaDB [mysql]> select user,password,host from user;
+------+-------------------------------------------+-----------------------+
| user | password | host |
+------+-------------------------------------------+-----------------------+
| root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | localhost |
| root | | localhost.localdomain |
| root | | 127.0.0.1 |
| root | | ::1 |
| | | localhost |
| | | localhost.localdomain |
| rep | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 192.168.42.% |
| rep1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 192.168.42.% |
+------+-------------------------------------------+-----------------------+
然后这里就出现了很多空密码,甚至还有空用户空密码可以直接登录,安全度不高。
这里由于我是测试数据,所以密码比较简单,如果是重要的服务器,一定要使用复杂密码,切记切记!!
####5、登录数据库
mysql -uroot -p123456
这里是一样的,在重要的服务器上,登录数据库的时候,不能这样暴露密码!
# 创建test_zxh数据库用作后面同步测试
create database test_zxh;
#查看数据库
show databases;
#创建一个测试表
CREATE TABLE Employee(
EpId INT,
EpName nvarchar(20),
EpSex char(2),
EpAdress nvarchar(20),
EpPhone nvarchar(20),
EpEmail nvarchar(20)
);
#插入测试数据
insert into Employee
value (6366,'laowang','男','菲律宾','18888888888','[email protected]');
#查询测试数据
select * from Employee;
在slave服务器安装MariaDB,但是不插入数据
服务器A(主) 192.168.42.133
服务器B(从) 192.168.42.134
Mysql版本:10.0.24
MariaDB [test_zxh]> status
--------------
mysql Ver 15.1 Distrib 10.0.24-MariaDB, for Linux (x86_64) using readline 5.1
Connection id: 6
Current database: test_zxh
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server: MariaDB
Server version: 10.0.24-MariaDB MariaDB Server
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /var/lib/mysql/mysql.sock
Uptime: 33 min 57 sec
在主服务器上为从服务器建立一个连接帐户,该帐户必须授予REPLICAITON SLAVE权限。
CREATE USER 'rep1'@'192.168.42.%' IDENTIFIED BY '123456';
grant replication slave on *.* to 'rep1'@'192.168.42.%' identified by '123456';
flush privileges;
vi /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
server-id=1
log-bin=/var/lib/mysql/mysql-bin
[mysqld_safe]
log-error=/var/log/mysqld.log
注意:因为这是在自己的虚拟机里面测试,所以需要配置,一般在服务器中已经开启binlog。
######②.重启服务
[root@localhost mysql]# service mysql restart
Shutting down MySQL... SUCCESS!
Starting MySQL. SUCCESS!
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 312 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
server-id=3
log-bin=/var/lib/mysql/mysql-bin
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
######②.重启服务
[root@localhost scripts]# service mysql restart
ERROR! MySQL server PID file could not be found!
Starting MySQL.. SUCCESS!
[root@localhost scripts]#
主服务器导出:
#锁表
MariaDB [(none)]> FLUSH TABLES WITH READ LOCK;
#导出test_zxh这个数据库
mysqldump --master-data -uroot -p test_zxh > test_zxh.sql
#查看master状态
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 312 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
#解除锁表状态
MariaDB [(none)]> UNLOCK TABLES;
(更新之后的方法)
因为mysqldump导出sql的时候自动会去锁表,而且带了–master-data就能把master的position放在sql里面一起执行,所以只需要一步导出就可以,考虑到生产环境中数据大的问题,所以用管道连接压缩,将到出来的sql压缩。
mysqldump --master-data -uroot -p test_zxh | gzip > test_zxh.sql.gz
从服务器导入:
#将导出的sql拷到本地
scp [email protected]:/var/lib/mysql/test_zxh.sql .
#创建test_zxh数据库
MariaDB [(none)]> create database test_zxh;
#导入数据
mysql -uroot -p test_zxh < test_zxh.sql
#查看数据是否导入成功
MariaDB [test_zxh]> select * from Employee;
+------+--------------+-------+----------+-------------+------------------+
| EpId | EpName | EpSex | EpAdress | EpPhone | EpEmail |
+------+--------------+-------+----------+-------------+------------------+
| 6366 | laowang | ? | 菲律宾 | 18888888888 | [email protected] |
+------+--------------+-------+----------+-------------+------------------+
**(更新后的方法)**直接将压缩的sql边解压边执行,这样能大大增大效率,这里还是使用管道。
#解压并导入数据
cat test_zxh.sql.gz | gunzip | mysql -uroot -p test_zxh
#查看数据插入是否成功
MariaDB [test_zxh]> select * from Employee;
Empty set (0.00 sec)
MariaDB [test_zxh]> select * from Employee;
+------+--------------+-------+----------+-------------+------------------+
| EpId | EpName | EpSex | EpAdress | EpPhone | EpEmail |
+------+--------------+-------+----------+-------------+------------------+
| 6366 | laowang | ? | 菲律宾 | 18888888888 | [email protected] |
+------+--------------+-------+----------+-------------+------------------+
1 row in set (0.00 sec)
从服务器上开启slave进行同步:
#配置slave信息
CHANGE MASTER TO
MASTER_HOST='192.168.42.133',
MASTER_PORT=3306,
MASTER_USER='rep1',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=312;
#启动slave
MariaDB [(none)]> start slave;
#查看是否同步
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.42.133
Master_User: rep1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 979
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 1202
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: 979
Relay_Log_Space: 1503
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: 1
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
1 row in set (0.00 sec)
完成从服务器的同步。