实验:二进制安装MySQL-5.7
- 百度云下载
链接:https://pan.baidu.com/s/1ZDCUcKjUbQDab0iH7v-32Q
提取码:663v
也可以直接官方下载
[centos]$ wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.26-el7-x86_64.tar.gz
- 解压
[centos]$ tar xf mysql-5.7.26-el7-x86_64.tar.gz -C /usr/local/
- 创建用户
[centos]$ useradd -r -s /sbin/nologin mysql
- 创建软链接
[centos]$ cd /usr/local/
[centos]$ ln -s mysql-5.7.26-el7-x86_64/ mysql
- 添加环境变量
[centos]$ echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[centos]$ source /etc/profile.d/mysql.sh
- 创建文件夹,用来放数据库文件
[centos]$ mkdir /data/mysql
#注意权限
[centos]$ chown -R mysql.mysql /data/mysql
- 初始化msyql,同时生成数据库文件
[centos]$ mysqld --initialize --user=mysql --datadir=/data/mysql
#最后一行这里,生成了密码,记住它
2019-07-12T07:57:15.212425Z 1 [Note] A temporary password is generated for root@localhost: LV)i(TYgl2XK
- 看,数据库文件有了
[centos]$ ll /data/mysql
-rw-r----- 1 mysql mysql 56 Jul 12 15:57 auto.cnf
-rw-r----- 1 mysql mysql 419 Jul 12 15:57 ib_buffer_pool
-rw-r----- 1 mysql mysql 12582912 Jul 12 15:57 ibdata1
-rw-r----- 1 mysql mysql 50331648 Jul 12 15:57 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 Jul 12 15:57 ib_logfile1
drwxr-x--- 2 mysql mysql 4096 Jul 12 15:57 mysql
drwxr-x--- 2 mysql mysql 8192 Jul 12 15:57 performance_schema
drwxr-x--- 2 mysql mysql 8192 Jul 12 15:57 sys
- 接下来修改配置文件
[centos]$ vim /etc/my.cnf
[client]
socket=/data/mysql/mysql.sock
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
- 把服务脚本拷贝到init.d目录下,这样我们就可以用systemctl来启动和关闭了
[centos]$ cp support-files/mysql.server /etc/init.d/mysqld
#添加
[centos]$ chkconfig --add mysqld
#启动服务
[centos]$ service mysqld start
- 初始化的时候生成的密码太复杂了,修改下它
[centos]$ mysqladmin -uroot -p"LV)i(TYgl2XK" password centos
实验:GTID
要做这个实验,你得准备2个主机,MySQL5.7,5.6不支持哦。
主服务器: 192.168.99.101 |
- 修改配置文件让主服务器支持GTID
[centos]$ vim /etc/my.cnf
server-id=1
log-bin=mysql-bin
gtid_mode=ON
enforce_gtid_consistency
重启它
[101]$ service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
- 创建用来连接的帐号
mysql> grant replication slave on *.* to 'repluser'@'%' identified by '123';
从服务器: 192.168.99.102 |
- 修改配置文件,开启GTID功能
[centos]$ vim /etc/my.cnf
server-id=2
gtid_mode=ON
enforce_gtid_consistency
- 连接主服务器
[centos]$ mysql -uroot -p123
mysql> CHANGE MASTER TO MASTER_HOST='192.168.99.103',
MASTER_USER='repluser',
MASTER_PASSWORD='123',
MASTER_PORT=3306,
MASTER_AUTO_POSITION=1;
- 启动之
mysql> start slave;
- 启动后看下状态
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.99.103
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: my-bin.000002
Read_Master_Log_Pos: 194
Relay_Log_File: localhost-relay-bin.000003
Relay_Log_Pos: 401
Relay_Master_Log_File: my-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
... ... ...
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: a9b9eedf-a47a-11e9-bc8d-000c293fae31
Master_Info_File: /data/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
... ... ...