您的位置:首页 > 数据库 > MySQL

Mysql主从配置,实现读写分离

2017-04-01 17:56 639 查看
作者:【吴业亮】云计算开发工程师

博客:http://blog.csdn.net/wylfengyujiancheng

一、安装数据库

1、安装软件包

[master&slave]# yum -y install mariadb-server


2、修改配置文件/etc/my.cnf

[mysqld]
character-set-server=utf8


3、启动服务并设置开机启动

[master&slave]# systemctl start mariadb
[master&slave]# systemctl enable mariadb


4、初始化数据库

[master&slave]# mysql_secure_installation

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):
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
Set root password? [Y/n] y
New password:
Re-enter new password:
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
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
Disallow root login remotely? [Y/n] y
... Success!

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
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
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!


5、配置防火墙

[master&slave]# firewall-cmd --add-service=mysql --permanent
success
[master&slave]# firewall-cmd --reload
Success


二、配置master节点

1、修改配置文件/etc/my.cnf

[root@master~]# vi /etc/my.cnf
[mysqld]
#定义二进制logs
log-bin=mysql-bin
# 定义server的ID
server-id=101


2、重启数据库

[root@master~]# systemctl restart mariadb


3、创建集群用户

[root@master~]# mysql -u root -p
MariaDB [(none)]> grant replication slave on *.* to replica@'%' identified by 'password';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> exit
Bye


三、配置slave节点

1、修改配置文件/etc/my.cnf

[mysqld]
#定义二进制logs
log-bin=mysql-bin
# 定义server的ID
server-id=102
#只读模式
read_only=1
# 定义主机名
report-host=slave


2、重启数据库

[root@slave ~]# systemctl restart mariadb


四、导出master的数据

[root@master ~]# mysql -u root -p
锁定所有的表
MariaDB [(none)]> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
#
查看状态并记录File和Position值
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      465 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)


导出数据

[root@master ~]# mysqldump -u root -p --all-databases --lock-all-tables --events > mysql_dump.sql


解锁

MariaDB [(none)]> unlock tables;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> exit


将数据拷贝到slave节点上

[root@master ~]# scp mysql_dump.sql slave:/tmp/


五、将master的数据导入到slave上

[root@slave ~]# mysql -u root -p < /tmp/mysql_dump.sql
Enter password:


配置集群参数

[root@slave ~]# mysql -u root -p
MariaDB [(none)]> change master to
-> master_host='172.16.8.90',     # Master Hosts's IP
-> master_user='replica',     # replication ID
-> master_password='password',     # replication ID's password
-> master_log_file='mysql-bin.000001',     # File value confirmed on Master
-> master_log_pos=465;     # Position value confirmed on Master
# 启动slave
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
# 查看状态
MariaDB [(none)]> show slave status\G
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: