主配置环境(准备工作)
//移动至文件夹下
[root@localhost ~]# cd /usr/local/src/
//查看
[root@localhost src]# ls
mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz
//解压
[root@localhost src]# tar -zxf mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# ls
mysql-5.6.47-linux-glibc2.12-x86_64 mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz
//将文件移动到MySQL下
[root@localhost src]# mv mysql-5.6.47-linux-glibc2.12-x86_64 /usr/local/mysql
[root@localhost src]#
[root@localhost src]# cd /usr/local/mysql/
[root@localhost mysql]#
[root@localhost mysql]#
[root@localhost mysql]# ls
bin data docs include lib LICENSE man mysql-test README scripts share
//改个主机名字没啥用
[root@localhost mysql]# vim /etc/hostname
[root@localhost mysql]# bash
[root@bai mysql]#
//创建一个MySQL用户
[root@bai mysql]# useradd mysql
[root@bai mysql]#
//创建一个存放MySQL数据的目录
[root@bai mysql]# mkdir /data/
//安装脚本依赖
[root@bai mysql]# yum install perl-Module-Install -y
//编译安装 --指定用户 --指定数据目录
[root@bai mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
//验证
[root@bai mysql]# echo $?
0
//复制配置文件
[root@wang mysql]# cp support-files/my-default.cnf /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"? y
//更改配置文件
[root@wang mysql]# vim /etc/my.cnf
basedir = /usr/local/mysql
datadir = /data/mysql
port = 3306
server_id = 156
socket = /tmp/mysql.sock
//拷贝并发送给从服务器
[root@wang mysql]# scp /etc/my.cnf root@192.168.142.157:/etc/
The authenticity of host '192.168.142.157 (192.168.142.157)' can't be established.
ECDSA key fingerprint is 8b:b3:c9:67:cb:cc:e9:c8:f1:38:eb:01:2f:cf:60:89.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.142.157' (ECDSA) to the list of known hosts.
root@192.168.142.157's password:
my.cnf 100% 1141 1.1KB/s 00:00
//复制启动文件
[root@wang mysql]# cp support-files/mysql.server /etc/init.d/mysqld
//修改启动文件
[root@wang mysql]# vim /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/data/mysql
//将启动文件拷贝发送给从服务器
[root@wang mysql]# scp /etc/init.d/mysqld root@192.168.142.157:/etc/init.d/
root@192.168.142.157's password:
mysqld 100% 10KB 10.3KB/s 00:00
//更改完配置文件后重启
[root@wang mysql]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/data/mysql/wang.err'.
. SUCCESS!
//检查是否启动MySQL端口 可以下载依赖
[root@wang mysql]# ps -ef |grep mysql
root 10552 1 0 17:33 pts/1 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/wang.pid
mysql 10729 10552 2 17:33 pts/1 00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=wang.err --pid-file=/data/mysql/wang.pid --socket=/tmp/mysql.sock --port=3306
root 10783 10488 0 17:34 pts/1 00:00:00 grep --color=auto mysql
搭建从配置环境
//移动
[root@localhost ~]# cd /usr/local/src/
//查看压缩包
[root@localhost src]# ls
mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz
//解压
[root@localhost src]# tar -zxf mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# ls
mysql-5.6.47-linux-glibc2.12-x86_64 mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz
//将解压完的文件移动到MySQL目录下
[root@localhost src]# mv mysql-5.6.47-linux-glibc2.12-x86_64 /usr/local/mysql
//移动
[root@localhost src]# cd /usr/local/mysql/
[root@localhost mysql]# ls
bin data docs include lib LICENSE man mysql-test README scripts share
//改个主机名
[root@localhost mysql]# vim /etc/hostname
[root@localhost mysql]# bash
//创建一个MySQL用户
[root@bai mysql]# useradd mysql
//创建一个存放MySQL数据的目录
[root@bai mysql]# mkdir /data/
//安装脚本依赖
[root@bai mysql]# yum install perl-Module-Install -y
//编译安装
[root@bai mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
//验证是否出错
[root@bai mysql]# echo $?
0
[root@bai mysql]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/data/mysql/bai.err'.
. SUCCESS!
[root@bai mysql]# ps -ef |grep mysql
root 10552 1 0 17:33 pts/1 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/bai.pid
mysql 10729 10552 2 17:33 pts/1 00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=bai.err --pid-file=/data/mysql/bai.pid --socket=/tmp/mysql.sock --port=3306
root 10783 10488 0 17:34 pts/1 00:00:00 grep --color=auto mysql
主服务器开始配置
//更改配置文件 (注意log_bin这是记录主服务器的操作的 有他才能进行剩下的操作)
[root@wang mysql]# vim /etc/my.cnf
//名字随便起
log_bin=bailinux1
server_id = 156
//更改完配置文件要重启
[root@wang mysql]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
[root@wang mysql]# cd /data/mysql/
[root@wang mysql]#
[root@wang mysql]# ls
//如果没有这两个文件后面的主从配置就没办法做了
auto.cnf ib_logfile1 test bai1.000001
ibdata1 mysql wang.err bai1.index
ib_logfile0 performance_schema wang.pid
//将MySQL所需的环境变量定义一下 不定义的话进入MySQL需要带着路径/usr/local/mysql/bin/进入MySQL
[root@wang mysql]# export PATH=$PATH:/usr/local/mysql/bin/
//把mysql库备份并恢复成ytl库,作为测试数据做了一个备份
[root@wang mysql]# mysqldump -uroot mysql > /tmp/mysql.sql
//定义完MySQL环境变量后要把他写入vim /etc/profile配置文件中使他永久生效
[root@wang mysql]# vim /etc/profile
//最后一行填写
export PATH=$PATH:/usr/local/mysql/bin/
//保存一下
[root@wang mysql]# source /etc/profile
//启动
[root@wang mysql]# chkconfig mysqld on
//这是那个备份的文件
[root@wang mysql]# ls -la /tmp/mysql.sql
-rw-r--r--. 1 root root 698597 12月 8 18:04 /tmp/mysql.sql
//接下来创建一个用户
[root@wang mysql]# mysql -uroot -e "create database bsq"
// 把备份文件导进去
[root@wang mysql]# mysql -uroot bsq < /tmp/mysql.sql
[root@wang mysql]# ls -la
总用量 111652
drwx------. 6 mysql mysql 4096 12月 8 18:21 .
drwxr-xr-x. 3 root root 18 12月 8 16:51 ..
-rw-rw----. 1 mysql mysql 56 12月 8 17:33 auto.cnf
-rw-rw----. 1 mysql mysql 12582912 12月 8 18:22 ibdata1
-rw-rw----. 1 mysql mysql 50331648 12月 8 18:22 ib_logfile0
-rw-rw----. 1 mysql mysql 50331648 12月 8 16:51 ib_logfile1
drwx------. 2 mysql mysql 4096 12月 8 16:51 mysql
drwx------. 2 mysql mysql 4096 12月 8 16:52 performance_schema
drwx------. 2 mysql mysql 6 12月 8 16:51 test
-rw-rw----. 1 mysql mysql 7606 12月 8 17:52 wang.err
-rw-rw----. 1 mysql mysql 6 12月 8 17:52 wang.pid
drwx------. 2 mysql mysql 4096 12月 8 18:22 bsq
-rw-rw----. 1 mysql mysql 698600 12月 8 18:22 bai1.000001
-rw-rw----. 1 mysql mysql 18 12月 8 17:52 bai1.index
这时我们发现ytlinux1.000001的字节大小变成698600大小了这是说明它把我们的创建库的过程
完全的记录了下来(这时我们就会想到我们是不是可以把刚创建的这个库和库文件删除,然后再通过这个二进制备份文件恢复呢? 当然可以,只要保证数据的完整性就可以了)
//创建用作同步数据库的用户
[root@wang mysql]# mysql -uroot
//授权
mysql> grant replication slave on *.* to 'repl'@192.168.142.157 identified by 'password';
Query OK, 0 rows affected (0.00 sec)
//锁住,目前的数距保持当前的状态。这样才能保持数据的一致性
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.01 sec)
//查看当前的状态
mysql> show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| bai1.000001 | 698812 | | | |
+-----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> quit
Bye
//把主服务器上的库复制到从服务器上
[root@wang mysql]# scp /tmp/mysql.sql root@192.168.142.157:/tmp/
root@192.168.142.157's password:
mysql.sql 100% 682KB 682.2KB/s 00:00
//查看大小
[root@wang mysql]# ls -la /tmp/mysql.sql
-rw-r--r--. 1 root root 698597 12月 8 18:04 /tmp/mysql.sql
从服务器开始配置
//定义mysql环境变量
[root@bai ~]# export PATH=$PATH:/usr/local/mysql/bin/
//把MySQL环建变量的路径写入配置文件
[root@bai ~]# vim /etc/profile
//写配置文件的最后面即可
export PATH=$PATH:/usr/local/mysql/bin/
//
[root@bai ~]# source /etc/profile
//启动
[root@bai ~]# chkconfig mysqld on
//修改配置文件
[root@bai ~]# vim /etc/my.cnf
server_id = 157
[root@bai ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
//这是通过主服务器复制过来的库,看看于主服务上上的大小是否一致
[root@bai mysql]# ls -la /tmp/mysql.sql
-rw-r--r--. 1 root root 698597 12月 8 19:40 /tmp/mysql.sql
//把数据库到同名库里面
[root@bai ~]# mysql -uroot
//创建库
mysql> create database bsq;
Query OK, 1 row affected (0.00 sec)
mysql> quit
Bye
[root@bai ~]# mysql -uroot bsq < /tmp/mysql.sql
[root@bai ~]#
[root@bai ~]#
[root@bai ~]# mysql -uroot
//查看库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| bsq |
+--------------------+
5 rows in set (0.00 sec)
//切换库
mysql> use ytl;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>
//查看表
mysql> show tables;
+---------------------------+
| Tables_in_bsq |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
28 rows in set (0.00 sec)
mysql> quit
Bye
[root@bai ~]# cd /data/mysql/
[root@bai mysql]# ls -la
总用量 110624
drwx------. 6 mysql mysql 4096 12月 8 21:41 .
drwxr-xr-x. 3 root root 18 12月 8 16:52 ..
-rw-rw----. 1 mysql mysql 56 12月 8 17:36 auto.cnf
-rw-rw----. 1 mysql mysql 7602 12月 8 19:33 bai.err
-rw-rw----. 1 mysql mysql 6 12月 8 19:33 bai.pid
-rw-rw----. 1 mysql mysql 12582912 12月 8 21:45 ibdata1
-rw-rw----. 1 mysql mysql 50331648 12月 8 21:45 ib_logfile0
-rw-rw----. 1 mysql mysql 50331648 12月 8 16:52 ib_logfile1
drwx------. 2 mysql mysql 4096 12月 8 16:52 mysql
drwx------. 2 mysql mysql 4096 12月 8 16:52 performance_schema
drwx------. 2 mysql mysql 6 12月 8 16:52 test
drwx------. 2 mysql mysql 4096 12月 8 21:45 bsq
//实现它的主从
//
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)
//
mysql> change master to master_host='192.168.142.156',master_user='repl',master_password='000000', master_log_file='bai1.000001',master_log_pos=698812;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
//
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
//
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.142.156
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: bai1.000001
Read_Master_Log_Pos: 410
Relay_Log_File: client-relay-bin.000002
Relay_Log_Pos: 277
Relay_Master_Log_File: bai1.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: // 同步那些库 这些配置都是可以写在著配置文件的my.cnf
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: 410
Relay_Log_Space: 451
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: 41
Master_UUID: 197dfb61-1310-11ec-af29-000c29b5c42c
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 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)