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

安装mysql 配置主从(详解)

2018-01-04 11:26 531 查看
由于最近工作需要,配置个主从

1、查看系统有没有mysql数据库

rpm -aq | grep -i mysql
rpm -e MySQL-server-5.6.27-1.el6.x86_64   #删除
yum remove mysql mysql-server mysql-libs compat-mysql51  #yum删除mysql
whereis mysql  #查看mysql的位置
rm -rf /usr/lib64/mysql   #删除
2、安装mysql

wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm  #下载rpm包
# rpm -ivh mysql-community-release-el7-5.noarch.rpm
# yum install mysql-community-server  #yum安装
3、安装mysql成功之后重启mysql服务

systemctl start mysqld.service
4、编辑mysql密码

mysql -u root
use mysql;
update user set password=password('123') where user='root'
flush privileges;
5、安装成功之后,有几个重要的目录
#数据库目录
/var/lib/mysql/
#配置文件
/usr/share/mysql(mysql.server命令及配置文件)
#相关命令
/usr/bin(mysqladmin mysqldump等命令)
#启动脚本
/etc/rc.d/init.d/(启动脚本文件mysql的目录)

6、哎呦刚才密码忘了怎么办

(1)登录到数据库所在服务器,手工kill吊mysql进程:

kill `cat /mysql-data-directory/hostname.pid`


其中,/mysql-data-directory/hostname.pid指的是 MySQL 数据目录下的.pid 文件,它记录了

MySQL 服务的进程号。
(2)使用--skip-grant-tables选项重启
MySQL 服务:

[root@localhost mysql]#  ./bin/mysqld_safe --skip-grant-tables --user=zzx & [1] 20881
[root@localhost mysql]# Starting mysqld daemon with databases from /home/zzx/mysql/data


其中--skip-grant-tables 选项前面曾经介绍过,意思是启动 MySQL 服务的时候跳过权限表认证。

启动后,连接到 MySQL的root将不需要口令。
(3)用空密码的 root 用户连接到 MySQL,并且更改 root 口令:

[root@localhost mysql]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 53
Server version: 5.0.41-community-log MySQL Community Edition (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
mysql> set password = password('123');
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> update user set password=password('123') where user='root' and host='localhost';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

此时,由于使用了--skip-grant-tables选项启动,使用“setpassword”命令更改密码失败,直接更新 user 表的 password 字段后更改密码成功。

(4)刷新权限表,使得权限认证重新生效:

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
(5)重新用 root 登录时,必须输入新口令

注意:在 MySQL中,密码丢失后无法找回,只能通过上述方式修改密码。

7、接来下,就开始安装主从

主机A:192.168.1.1(master)

主机B:192.168.1.2(slave)
首先,确定两台机子之间可以ping通,

Master的配置

编辑mysql配置文件 :

vi /etc/my.cnf

log-bin=mysql-bin
server-id=2  #用于标识唯一的数据库 确保唯一
binlog-ignore-db=information_schema  #表示同步的时候ignore的数据库
binlog-ignore-db=cluster
binlog-ignore-db=mysql
binlog-do-db=ufind_db  #指定需要同步的数据库
重启mysql,进去mysql,赋予从库权限账号,允许用户在出库上读取日志,赋予slave机器有file、replication slave权限才行

在master数据裤上

>GRANT FILE ON *.* TO 'root'@'192.168.1.2' IDENTIFIED BY 'mysql password';
>GRANT REPLICATION SLAVE ON *.* TO 'root'@'192.168.1.2' IDENTIFIED BY 'mysql password';
>FLUSH PRIVILEGES
重启mysql 登录mysql 显示主库信息:

mysql> show master status;
+------------------+----------+--------------+----------------------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB                 | Executed_Gtid_Set |
+------------------+----------+--------------+----------------------------------+-------------------+
| mysql-bin.000004 |   120    | ufind_db     | information_schema,cluster,mysql |                  |
+------------------+----------+--------------+----------------------------------+-------------------+
1 row in set (0.00 sec)
这里的file 和possition在从库上用到的

slave的配置

1.从库的配置,先修改配置文件:

log-bin=mysql-bin
server-id=3
binlog-ignore-db=information_schema
binlog-ignore-db=cluster
binlog-ignore-db=mysql
replicate-do-db=ufind_db
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
slave-net-timeout=60
2、重启mysql,进入mysql执行以下操作
mysql> stop slave;  #关闭Slave
mysql> change master to master_host='192.168.1.1'
,master_user='root',
master_password='123456',
master_log_file='mysql-bin.000004', master_log_pos=28125;

mysql> start slave;  #开启Slave
在这里指定Master的信息,master_log_file是在配置Master的时候的File选项,master_log_pos是在配置Master的Position

选项,这里要进行对应

然后通过show slave status;查看配置信息

mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.167.1.1
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 28125
Relay_Log_File: VM_128_194_centos-relay-bin.000004
Relay_Log_Pos: 26111
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: ufind_db
Replicate_Ignore_DB: mysql
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: 28125
Relay_Log_Space: 26296
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: 2
Master_UUID: 8ac3066a-9680-11e5-a2ec-5254007529fd
Master_Info_File: /data/mysqldb/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)

ERROR:
No query specified

mysql>


可以看到,已经配置成功。

在显示的这些信息中,我们主要关心“Slave_IO_Running”和“Slave_SQL_Running”这两个进程状态是否是“yes”,

这两个进程的含义分别如下。 l  Slave_IO_Running:此进程负责从服务器(Slave)从主服务器(Master)上读取 BINLOG

日志,并写入从服务器上的中继日志中。

l  Slave_SQL_Running:此进程负责读取并且执行中继日志中的 BINLOG日志。

只要其中有一个进程的状态是no,则表示复制进程停止,错误原因可以从“Last_Errno”字段的值中看到。

除了查看上面的信息,用户还可以通过这个命令了解从服务器的配置情况以及当前和主服务器的同步情况,包括指向那个主服

务器,主服务器的端口,复制使用的用户,当前日志恢复到的位置等,这些信息都是记录在从服务器这一端的,主服务器上并

没有相应的信息。


添加需要同步的从库Slave

由于种种原因,测试的时候使用test库,这里我按照上述的方式,修改Master的my.cnf的配置文件,新增同步的数据库test,重

启MySQL,执行Master的:
show master status
如下:



相应的,要修改Slave从库的信息在my.cnf 增加 
replicate-do-db=test
,重启Mysql,根据上述的show master status,在Slave
从库中执行下边的内容:

>stop slave
>change master to master_host='192.168.1.1',master_user='root',master_password='123456',master_log_file='mysql-bin.000005', master_log_pos=120;
>start slave
继续使用 show slave status;

mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.1
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 1422
Relay_Log_File: VM_128_194_centos-relay-bin.000004
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: ufind_db,test
Replicate_Ignore_DB: mysql
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: 1422
Relay_Log_Space: 468
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: 2
Master_UUID: 8ac3066a-9680-11e5-a2ec-5254007529fd
Master_Info_File: /data/mysqldb/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)

ERROR:
No query specified

mysql>

是否启用bin-log日志:

show variables like 'log_bin';

查看所有日志日志:

show binary logs;

查看某个日志文件里面的操作:

show binlog events in 'mysql-bin.000001'

至此,配置就完成了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: