Centos7 下Mysql5.7.18的安装和简单主从
时间:2022-03-15 04:34
Mysql5.7.18的安装
#下载解压tar包
wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
tar -zxvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
cd /usr/local/ && mv mysql-5.7.17-linux-glibc2.5-x86_64 mysql
cd /usr/local/mysql && mkdir data
cd ..
groupadd mysql
useradd -r -s /sbin/nologin -g mysql mysql
chown -R mysql:mysql mysql
cd
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
mkdir -p /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql
#修改mysql的启动文件如下
vim /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
#注册和初始化mysql数据库
cd /usr/local/mysql/bin
./mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize-insecure
(没有生成root密码,去掉-insecure会生成root密码,在最后一行)
mv /etc/my.cnf /etc/my.cnf.bak
vim /etc/my.cnf
(自建mysql的配置文件,添加下面的几项)
[mysqld]
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
# 一般配置选项
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
socket = /var/lib/mysql/mysql.sock
pid-file=/usr/local/mysql/mysql.pid
character-set-server=utf8
default-storage-engine=INNODB
[mysql]
socket = /var/lib/mysql/mysql.sock
no-auto-rehash
default-character-set=utf8
(以上为简单配置项,具体依据实际配置)
/etc/init.d/mysqld start
#重置root密码
cd /usr/local/mysql/bin
(方便使用时可建立ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql软连接)
./mysql -u root -p
set password=password(‘你想设置的root密码‘);
flush privileges;
exit;
主从配置
两台服务器IP
master:192.168.0.1
slave: 192.168.0.2
master主机上的操作;
修改配置文件
vim /etc/my.cnf
添加下面几行
[mysqld]
server-id=1(服务器的唯一指定id)
log-bin=mysql-bin(开启二进制日志)
/etc/init.d/mysqld restart(重启MySQL)
./mysql -uroot -p
>grant repliaction slave on *.* to mysync@‘192.168.0.2‘ identified by ‘123456‘;
>flush privileges;
>show master status;(查看主机状态)
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001| 103 | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
(不要做任何操作,防止master的数据更改)
slave从服务器的操作
vim /etc/my.cnf
添加下面几行
[mysqld]
server-id=2
log-bin=mysql-bin(不是必须的,开启比较好一些)
/etc/init.d/mysql restart(重启MySQL)
./mysql -uroot -p
>change master to master_host=192.168.0.1,master_user=‘mysync‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000001‘,master_log_pos=103(主机中查询的数值);
>start slave;
>show slave status\G(查询从服务器的链接情况,必须要满足两个‘yes‘)
Slave_IO_State: Waiting for master to send event //I/O状态
Master_Host: 192.168.0.1 //主服务器ip
Master_User: mysync //具有复制权限的用户
Master_Port: 3306 //默认监听的端口
Connect_Retry: 60 //重试时间
Master_Log_File: mysql-bin.000001 //读取的二进制日志文件
Read_Master_Log_Pos: 103 //二进制日志中的位置
Relay_Log_File: relay-bin.000002 //当前读取的中继日志文件
Relay_Log_Pos: 581 //中继日志中事件的位置
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes //I/O线程已启动
Slave_SQL_Running: Yes //SQL线程已启动
................................
................................
Seconds_Behind_Master: 0 //落后主服务器的时间,单位秒
>exit;
(可以使用mysql> help change master to 查看需要的命令)
下面就是测试了在主库上建新库,查看从库是否也生成同样的库。
以上是基本的配置,详细配置可根据自己的需求来添加参数。