######RHEL6.5下MySQL5.7.26的详细安装过程######
OS:RHEL6.5 64bit
DB:mysql-5.7.26
第一步:下载、上传、解压
- 地址:https://dev.mysql.com/downloads/mysql/5.7.html#downloads
Product Version :6.7.26
Operating system:linux-Generic
OS version: linux-Generic(glibc2.12)(x86,64-bit)
(mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz) - 上传解压
(1)进入 /usr/local 目录,上传下载好的压缩包(我习惯通过 rz 命令进行本地文件上传)
[root@localhost /]# cd /usr/local/
[root@localhost local]# rz
(2)接着进行解压
[root@localhost local]# tar -zxvf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
1
(3)解压后由于文件夹名太长,为了方便给其重命名为 mysql(所以目录结构为 /usr/local/mysql)
[root@localhost local]# mv mysql-5.7.26-linux-glibc2.12-x86_64 mysql
1
(4)删除原压缩包(毕竟好几百兆,没必要留着占地方)
[root@localhost local]# rm -f mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
第二步:安装
- 检查是否已存在 mysql 相关
[root@localhost local]# rpm -qa | grep mysql
1
如果显示出 mysql-xx 什么的,对其删除,指令为:rpm -e --nodeps 显示出来的文件名,接着检查 mariadb
[root@localhost local]# rpm -qa | grep mariadb
1
同样,若是显示 mariadb-xx ,也对其删除,指令同上
- 添加 mysql 用户组 和 mysql 用户(意思就是把创建的 mysql 用户添加到 mysql 组里)
[root@localhost local]# groupadd mysql
[root@localhost local]#useradd -g mysql mysql
3. 赋予权限(意思就是让 mysql 用户组和 mysql 用户有操作目录的权限)
[root@localhost local]# chown -R mysql mysql
[root@localhost local]# chgrp -R mysql mysql
chown -R mysql:mysql mysql
4. 创建配置文件(直接通过 vi 来创建,粘贴以下内容后 wq 保存退出,注意 basedir 和 datadir 等路径)
[root@localhost local]# cd mysql/
[root@localhost mysql]# vi /etc/my.cnf
[mysqld]
port=3306
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
user=mysql
tmpdir=/tmp
bind-address = 0.0.0.0
max_connections=200
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
default-storage-engine=INNODB
innodb_buffer_pool_size=64MB
max_allowed_packet=16M
skip-name-resolve
[mysqld_safe]
log-error=/usr/local/mysql/data/error.log
pid-file=/usr/local/mysql/data/mysql.pid
[mysql]
default-character-set=utf8mb4
[client]
socket=/tmp/mysql.sock
default-character-set=utf8mb4
————————————————
5. 初始化
(1)先安装个 libaio(有的虚拟机可能已经有了这个,反正执行一次不亏)
[root@localhost mysql]# yum -y install libaio
1
(2)创建 data 目录(按照上面的配置目录)
[root@localhost mysql]# mkdir data
1
(3)初始化(mysql目录 /usr/local/mysql ,重点是复制记住初始化生成的密码)
[root@localhost mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
[root@rmysql1 mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2020-07-27T09:26:33.664625Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-07-27T09:26:34.017535Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-07-27T09:26:34.068047Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-07-27T09:26:34.319076Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 43652f27-cfeb-11ea-83bf-005056bfb643.
2020-07-27T09:26:34.320041Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed’ cannot be opened.
2020-07-27T09:26:34.321955Z 1 [Note] A temporary password is generated for root@localhost: xI1hilllJc)9
第三步:启动配置
- 设置开机自启
(1)把 mysql.server 复制到 init.d/mysql 下(init.d下无mysql 文件夹也无妨,直接执行下面这句)
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysql
1
(2)添加可执行权限(这步可以不执行,直接走第三部)
[root@localhost mysql]# chmod +x /etc/init.d/mysql
1
(3)添加 mysql 服务
[root@localhost mysql]# chkconfig --add mysql
1
(4)查看服务列表(若 3 4 5 为 off,则执行:chkconfig --level 345 mysql on)
[root@localhost mysql]# chkconfig --list
(5)添加mysql系统命令
[root@localhost mysql]# vi /etc/profile
1
后面补上下面两句后再执行 source /etc/profile 使配置立即生效
export MYSQL_HOME=/usr/local/mysql
export PATH=PATH
1
2
[root@localhost mysql]# source /etc/profile
1
2. 启动 mysql 服务
(1)命令启动
[root@localhost mysql]# service mysql start
)登录(回车后再输入上面初始化生成的密码)
[root@localhost mysql]# ./bin/mysql -u root -p
3)修改密码(注意:在 mysql> 后输入的命令末尾要加分号;我的密码就设为 mysql,随意;第二句要对应所设的密码)
set password=password(‘mysql’);
grant all privileges on . to root@’%’ identified by ‘mysql’;
flush privileges;
(4)设置远程访问权限(执行完后,输入 quit 即可退出,退出可不加分号)
/*
use mysql;
update user set user.Host=’%’ where user.User=‘root’;
select host from user where user = ‘root’;
flush privileges;
*/
3. 重启 mysql 服务
[root@localhost mysql]# service mysql restart
1
主要命令
启动: service mysql start
关闭: service mysql stop
状态: service mysql status
############mysql 安装完毕##########
###MySQL5.7.26主从双向主从复制###
服务器规划
rmysql1 192.168.81.11
rmysql2 192.168.81.12
在rmysql1上操作
vi /etc/my.cnf
修改或添加下面这几行:
server-id=1
log-bin=mysql-bin # 启用二进制日志
auto-increment-increment = 2 #每次增长2
auto-increment-offset = 1 #设置自动增长的字段的偏移量
#两个可选参数(2选1):
binlog-do-db=db1,db2 #需要同步的库
binlog-ignore-db=db1,db2 #忽略不同步的库
保存后重启
/etc/init.d/mysqld restart
运行mysql客户端
mysql -uroot -p
创建同步账号
grant replication slave on . to ‘repl’@’%’ identified by ‘123456’;
flush privileges;
锁表,不让数据写入
flush tables with read lock;
show master status;
mysql> show master status;
±-----------------±---------±-------------±------------------------------------------------±------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
±-----------------±---------±-------------±------------------------------------------------±------------------+
| mysql-bin.000004 | 154 | t | mysql,information_schema,performance_schema,sys | |
±-----------------±---------±-------------±------------------------------------------------±------------------+
1 row in set (0.00 sec)
记录下二进制日志文件名和位置
备份数据库
在rmysql1上执行
mysqldump -uroot -p db1 > back.sql;
把备份好的数据拷贝到从服务器
scp back.sql 192.168.1.2:~
在rmysql2操作
vi /etc/my.cnf
修改或增加:
server-id=2 #这个数值不能和主一样
log-bin=mysql-bin # 启用二进制日志
auto-increment-increment = 2 #每次增长2
auto-increment-offset = 2 #设置自动增长的字段的偏移量
#可选参数(2选1,这两个参数设置成和主一样):
replicate-do-db=db1,db2
replicate-ignore-db=db1,db2
保存后重启
/etc/init.d/mysqld restart
把在rmysql1上备份的数据导入rmysql2
在rmysql2上执行
[root@rmysql2 ~]# mysqldump -uroot -p db1 < back.sql
运行mysql客户端
mysql -uroot -p
创建同步账号
grant replication slave on . to ‘repl’@’%’ identified by ‘123456’;
flush privileges;
show master status;
mysql> show master status;
±-----------------±---------±-------------±-----------------±------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
±-----------------±---------±-------------±-----------------±------------------+
| mysql-bin.000004 | 154 | | | |
±-----------------±---------±-------------±-----------------±------------------+
1 row in set (0.00 sec)
记录下二进制日志文件名和位置
执行以下命令
rmysql2服务器
stop slave;
change master to master_host=‘192.168.81.11’,master_user=‘repl’,master_password=‘123456’,master_log_file=‘mysql-bin.000004’,master_log_pos=154;
(master_log_file和master_log_pos填上刚才记录下的二进制日志文件名和位置)
start slave;
查看从服务器的状态:
show slave status\G
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.81.11
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 154
Relay_Log_File: rmysql2-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: t
Replicate_Ignore_DB: mysql,information_schema,performance_schema,sys
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: 154
Relay_Log_Space: 529
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_UUID: 43652f27-cfeb-11ea-83bf-005056bfb643
Master_Info_File: /usr/local/mysql/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
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
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
回到rmysql1上操作
运行mysql客户端
mysql -uroot -p
取消server1上数据库的锁定
mysql> unlock tables;
执行以下命令
stop slave;
change master to master_host=‘192.168.81.12’,master_user=‘repl’,master_password=‘123456’,master_log_file=‘mysql-bin.000004’,master_log_pos=154;
(master_log_file和master_log_pos填上刚才记录下的二进制日志文件名和位置)
start slave;
查看从服务器的状态:
show slave status\G
mysql> show slave status\G
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.81.12
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 154
Relay_Log_File: rmysql1-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000004
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: 154
Relay_Log_Space: 529
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: 5c48243f-d0a2-11ea-bf71-005056bf93a1
Master_Info_File: /usr/local/mysql/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
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
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
主主复制测试
经测试,主主复制配置成功。
mysql主从双向复制维护
主从服务器重启后
mysql数据库服务通过系统服务自动启动
mysql数据库数据主从双向同步需要下面的操作(主从都操作)
start slave;
在MySQL配置主从关系时,会用到start slave, stop slave命令,本文简单介绍两个命令的使用方法和作用。
start slave
mysql> start slave
不带任何参数,表示同时启动I/O 线程和SQL线程。
I/O线程从主库读取bin log,并存储到relay log中继日志文件中。
SQL线程读取中继日志,解析后,在从库重放。
stop slave
类似的,
mysql> stop slave
完成停止I/O 线程和SQL线程的操作。
常见问题**
slave have equal MySQL Server UUIDs原因及解决
MySQL主从复制架构的时候,碰到了"Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work." 这个错误提示。即主从架构中使用了相同的UUID。检查server_id系统变量,已经是不同的设置,那原因是?接下来为具体描述。
果然出现了重复,原因是克隆了虚拟机