下载安装包
Mysql下载地址:https://downloads.mysql.com/archives/community/
传输至目标机器
安装Mysql5.7
#删除自带mariadb数据库
[root@VM-24-15-centos local]# rpm -qa | grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
[root@VM-24-15-centos local]# rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
#添加用户组用户
[root@VM-24-15-centos local]# groupadd mysql
# -g 用户组,指定用户所属用户组
# -s shell文件,指定用户的登录shell /bin/false的意思是禁止某个用户登录
# -r 建立系统帐号。
[root@VM-24-15-centos local]# useradd -r -g mysql -s /bin/false mysql
#解压安装包
[root@VM-24-15-centos local]# tar xzvf mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz
[root@VM-24-15-centos local]# ln -s mysql-5.7.16-linux-glibc2.5-x86_64 mysql
[root@VM-24-15-centos local]# ll
总用量 626576
drwxr-xr-x. 2 root root 4096 8月 5 2020 bin
drwxr-xr-x. 2 root root 4096 4月 11 2018 etc
drwxr-xr-x. 2 root root 4096 4月 11 2018 games
drwxr-xr-x. 2 root root 4096 4月 11 2018 include
drwxr-xr-x. 2 root root 4096 4月 11 2018 lib
drwxr-xr-x. 2 root root 4096 4月 11 2018 lib64
drwxr-xr-x. 2 root root 4096 4月 11 2018 libexec
lrwxrwxrwx 1 root root 34 8月 3 10:10 mysql -> mysql-5.7.16-linux-glibc2.5-x86_64
drwxr-xr-x 9 root root 4096 8月 3 10:09 mysql-5.7.16-linux-glibc2.5-x86_64
-rw-r--r-- 1 root root 641555814 8月 2 16:44 mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz
drwxr-xr-x 14 root root 4096 6月 29 2022 qcloud
drwxr-xr-x 3 root root 4096 11月 15 2021 sa
drwxr-xr-x. 2 root root 4096 4月 11 2018 sbin
drwxr-xr-x. 5 root root 4096 3月 7 2019 share
drwxr-xr-x. 2 root root 4096 4月 11 2018 src
#创建数据文件存放路径
[root@VM-24-15-centos local]# mkdir -p /data/mysql/{mysql_3306,mysql_3307}
[root@VM-24-15-centos local]# mkdir /data/mysql/mysql_3306/{data,log,tmp}
[root@VM-24-15-centos local]# mkdir /data/mysql/mysql_3307/{data,log,tmp}
#更改目录权限
[root@VM-24-15-centos mysql]# chown -R mysql:mysql/data/mysql/
[root@VM-24-15-centos mysql]# chown -R mysql:mysql /usr/local/software/mysql/
#设置环境变量
[root@VM-24-15-centos mysql]# vim /etc/profile
#在文件末尾添加下面信息
export PATH=/usr/local/software/mysql/bin:$PATH
[root@VM-24-15-centos mysql]# source /etc/profile
修改my.cnf
#复制my.cnf文件到etc目录
[root@VM-24-15-centos mysql]# cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
[client]
port=3306
socket=/tmp/mysql.sock
[mysqld_multi]
user=root
pass=xxxx
mysqld=/usr/local/sofware/mysql/bin/mysqld_safe
mysqladmin=/usr/local/sofware/mysql/bin/mysqladmin
log=/data/mysql/mysqld_multi.log
[mysqld]
user=mysql
basedir=/usr/local/software/mysql
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[mysqld3306]
mysqld=mysqld
mysqladmin=mysqladmin
datadir=/data/mysql/mysql_3306/data
port=3306
server_id=3306
socket=/tmp/mysql_3306.sock
log-output=file
slow_query_log=1
long_query_time=1
slow_query_log_file=/data/mysql/mysql_3306/log/slow.log
log-error=/data/mysql/mysql_3306/log/error.log
binlog_format=mixed
log-bin=/data/mysql/mysql_3306/log/mysql3306_bin
[mysqld3307]
mysqld=mysqld
mysqladmin=mysqladmin
datadir=/data/mysql/mysql_3307/data
port=3307
server_id=3307
socket=/tmp/mysql_3307.sock
log-output=file
slow_query_log=1
long_query_time=1
slow_query_log_file=/data/mysql/mysql_3307/log/slow.log
log-error=/data/mysql/mysql_3307/log/error.log
binlog_format=mixed
log-bin=/data/mysql/mysql_3307/log/mysql3307_bin
初始化数据库
#初始化3306 (最后一段会生成临时密码,第一次登录mysql使用)
[root@VM-24-15-centos mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/software/mysql/ --datadir=/data/mysql/mysql_3306/data
2023-08-22T10:19:11.873968Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2023-08-22T10:19:11.874002Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
2023-08-22T10:19:11.874005Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set.
2023-08-22T10:19:12.100598Z 0 [Warning] InnoDB: New log files created, LSN=45790
2023-08-22T10:19:12.174425Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2023-08-22T10:19:12.242722Z 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: 56bbd92d-40d5-11ee-bf49-525400f88ff4.
2023-08-22T10:19:12.245420Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2023-08-22T10:19:12.245870Z 1 [Note] A temporary password is generated for root@localhost: Hl*g=Moss5d/
#初始化3307
[root@VM-24-15-centos mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/software/mysql/ --datadir=/data/mysql/mysql_3307/data
2023-08-22T10:19:21.306729Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2023-08-22T10:19:21.306763Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
2023-08-22T10:19:21.306766Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set.
2023-08-22T10:19:21.498322Z 0 [Warning] InnoDB: New log files created, LSN=45790
2023-08-22T10:19:21.557563Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2023-08-22T10:19:21.633708Z 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: 5c54cc97-40d5-11ee-8a3a-525400f88ff4.
2023-08-22T10:19:21.636155Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2023-08-22T10:19:21.636623Z 1 [Note] A temporary password is generated for root@localhost: omgj6jgIp/tW
启动多实例
#设置启动文件
cp /usr/local/software/mysql/support-files/mysql.server /etc/init.d/mysql
#查看实例状态
[root@VM-24-15-centos mysql]# mysqld_multi report
#查看单个实例状态
[root@VM-24-15-centos mysql]# mysqld_multi report 3306
#启动实例
[root@VM-24-15-centos mysql]# mysqld_multi start
#启动单个实例
[root@VM-24-15-centos mysql]# mysqld_multi start 3306
#停止实例
[root@VM-24-15-centos mysql]# mysqld_multi stop
#停止单个实例
[root@VM-24-15-centos mysql]# mysqld_multi stop 3306
修改初始密码
#使用初始化的时候的临时密码登录
[root@VM-24-15-centos mysql]# mysql -S /tmp/mysql_3306.sock -uroot -p
Enter password:
mysql> set password='xxxx';
[root@VM-24-15-centos mysql]# mysql -S /tmp/mysql_3307.sock -uroot -p
Enter password:
mysql> set password='xxxx';
设置可以远程登录(需要在各个节点上操作)
#3306,3307两个节点都需要设置
[root@VM-24-15-centos local]# mysql -S /tmp/mysql_3306.sock -uroot -pxxxx
(root@localhost) [mysql]> select host from user where user = 'root';
+-----------+
| host |
+-----------+
| localhost |
+-----------+
1 row in set (0.00 sec)
(root@localhost) [mysql]> update user set host = '%' where user = 'root';
(root@localhost) [mysql]> flush privileges;
设置主备(双主同步)
3306为主库,3307为从库
#在/etc/my.cnf添加如下参数
#1.在[mysqld3306]下面添加
#作为主节点提供备份
binlog-do-db=test
binlog-do-db=task
binlog-ignore-db=mysql
#作为从节点从别的节点备份
replicate-do-db=test
replicate-do-db=task
replicate-ignore-db=mysql
#2.在[mysqld3307]下面添加
#作为主节点提供备份
binlog-do-db=test
binlog-do-db=task
binlog-ignore-db=mysql
#作为从节点从别的节点备份
replicate-do-db=test
replicate-do-db=task
replicate-ignore-db=mysql
#3.登录3306节点 创建公共用户并授权
(root@localhost) [mysql]> CREATE USER 'test'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
(root@localhost) [mysql]> GRANT REPLICATION SLAVE ON *.* TO 'test'@'%';
(root@localhost) [mysql]> flush privileges;
#4.查看状态-要先重新启动
(root@localhost) [mysql]> show master status;
#5.登录3307节点对master配置,根据上一步查看master status
(root@localhost) [mysql]> change master to
MASTER_HOST='0.0.0.0',
MASTER_PORT=3306,
MASTER_USER='test',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql3306_bin.000005',
MASTER_LOG_POS=154;
#6.登录3307节点 创建公共用户并授权
(root@localhost) [mysql]> CREATE USER 'test'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
(root@localhost) [mysql]> GRANT REPLICATION SLAVE ON *.* TO 'test'@'%';
(root@localhost) [mysql]> flush privileges;
#4.查看状态-要先重新启动
(root@localhost) [mysql]> show master status;
#5.登录3306节点对master配置,根据上一步查看master status
(root@localhost) [mysql]> change master to
MASTER_HOST='0.0.0.0',
MASTER_PORT=3307,
MASTER_USER='test',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql3307_bin.000005',
MASTER_LOG_POS=154;
#6.两个节点开启同步并显示状态
(root@localhost) [mysql]> start slave;
(root@localhost) [mysql]> show slave status\G
#结束同步重置master
stop slave;
reset master;