一、安装mysql
官网下载:https://dev.mysql.com/downloads/mysql/
可选择要安装的版本,本次使用的是mysql5.7版本
准备两台主机
10.27.3.123(主节点)
10.27.3.124(从节点)
1.解压安装包并移动至指定位置
tar -zxvf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.28-linux-glibc2.12-x86_64 /usr/local/mysql
2.添加mysql用户并赋予权限
# 添加用户组
groupadd mysql
# 添加mysql用户
useradd -r -g mysql mysql
# 进入mysql目录
cd /usr/local/mysql/
# 赋予mysql用户权限
chown -R mysql:mysql ./
3.配置mysql
将mysql/support-files下的mysql.server 复制到 /etc/init.d/下并自定义为mysql
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
修改该服务
vim /etc/init.d/mysql
需要修改的内容
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
配置mysql的my.cnf配置文件
vim /etc/my.cnf
修改配置内容如下
[mysqld]
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql
port=3306
character-set-server=utf8
# 不区分大小写
lower_case_table_names=1
default_storage_engine=InnoDB
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mysql/mysql.log
pid-file=/var/run/mysql/mysql.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
4.初始化数据库
cd /usr/local/mysql/bin
./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
初始化完成会生成默认的密码在执行记录中,注意复制出来
5.启动mysql服务
service mysql start
可能会报错
[root@localhost bin]# service mysql start
Starting MySQL.Logging to '/var/log/mysql/mysql.log'.
2021-07-09T02:57:23.974548Z mysqld_safe Directory '/var/lib/mysql' for UNIX socket file don't exists.
ERROR! The server quit without updating PID file (/usr/local/mysql/data/localhost.localdomain.pid).
提示很明显,我们配置的路径不存在,创建对应的文件夹目录并赋予mysql用户权限
mkdir /var/log/mysql
chown -R mysql /var/log/mysql/
重新启动mysql服务
[root@localhost bin]# service mysql start
Starting MySQL. SUCCESS!
查看mysql服务状态
[root@localhost var]# service mysql status
SUCCESS! MySQL running (14369)
6.登录mysql 并修改默认密码
需要用到我们刚刚初始化数据库生成的密码
[root@localhost var]# mysql -uroot -p
-bash: mysql: command not found
如果提示命令不存在,建立软连接
ln -s /usr/local/mysql/bin/mysql /usr/bin
再次执行登录
[root@localhost var]# mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
发现了一个熟悉的东西,mysql.sock,连接localhost通常通过一个Unix域套接字文件进行,一般是/tmp/mysql.sock
查看本机是否有mysql.sock
[root@localhost var]# find / -name mysql.sock
/var/lib/mysql/mysql.sock
建立软连接
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
再次登录,成功!
[root@localhost var]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.28
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
修改密码
mysql> set password=password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
配置mysql开机自启
[root@localhost var]# chmod 755 /etc/init.d/mysql
[root@localhost var]# chkconfig --add mysql
[root@localhost var]# chkconfig --level 345 mysql on
开启mysql远程访问,否则无法远程连接mysql
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root用户的密码,自行修改';
flush privileges;
至此mysql安装完成,两台机器都需要安装!
二、主从搭建
搭建两台MySQL服务器(一主一从),一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作。
1.配置master节点
修改my.cnf配置文件
vim /etc/my.cnf
在[mysqld]中添加如下配置
# 要给从机同步的库(如果不写,默认全部同步,可不配置)
binlog-do-db=db01
# 不给从机同步的库(多个写多行,可不配置)
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=sys
# 自动清理 7 天前的log文件,可根据需要修改(可不配置)
expire_logs_days=7
# 启用二进制日志
log-bin=master-bin
# 服务器唯一ID,一般取IP最后一段
server-id=123
# 指定索引文件的位置
log_bin_index=master-bin.index
配置修改完后重启主节点
2.配置slave节点
修改my.cnf配置文件
vim /etc/my.cnf
在[mysqld]中添加如下配置
# 服务器唯一id
server-id=124
#加上以下参数可以避免更新不及时,SLAVE 重启后导致的主从复制出错
read_only=1
master_info_repository=TABLE
relay_log_info_repository=TABLE
relay-log = slave-relay-bin
#主服务器I/O日志读取、记录及存放
relay-log-index = slave-relay-bin.index
配置修改完后重启从节点
3.加入主节点
在主节点机器上登录mysql,查看状态
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 | 154 | | | |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
这里查看到的信息即是主服务器准许接入的信息,我们要在从服务器MySQL语句中执行接入语句需要用到主服务器允许接入参数,这里的file和position非常重要。
接下来在slave从节点上执行下述命令,命令解释如下
change master to master_host='主服务器ip地址',
master_port=3306,master_user='链接主服务器账户',
master_password='链接主服务器角色账户密码',
master_log_file='主服务器file参数',
master_log_pos=主服务器positon参数
mysql> change master to master_host='10.27.3.123',master_port=3306,master_user='root',master_password='hatech1618!@#',master_log_file='master-bin.000001',master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
启动slave从节点
start slave;
查看从节点状态
show slave status\G
代表slave从节点运行正常,主从复制搭建完毕。