1. 下载二进制安装包
https://downloads.mysql.com/archives/community/
下载地址:
https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.37-linux-glibc2.12-x86_64.tar
下载解压
wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.37-linux-glibc2.12-x86_64.tar
tar -xvf mysql-5.7.37-linux-glibc2.12-x86_64.tar
# 解压出来之后还有一个test的包,那个暂时先不管
tar -xvf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.37-linux-glibc2.12-x86_64 /usr/local/
cd /usr/local/
ln -sv mysql-5.7.37-linux-glibc2.12-x86_64 mysql
复制代码
添加 PATH 路径
# vim /etc/profile
export MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin
复制代码
配置生效与检查
source /etc/profile
mysql --version
复制代码
安装一些依赖包
yum -y remove mariadb*
yum -y install autoconf gcc glibc make openssl openssl-devel perl-JSON.noarch
复制代码
添加 mysql 用户
useradd -M -s /sbin/nologin mysql
id mysql
复制代码
创建数据目录
mkdir /home/my3306
chown -R mysql.mysql /home/my3306
复制代码
2. mysql 初始化与服务配置
mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/home/my3306/ --explicit_defaults_for_timestamp
复制代码
注意初始化密码
配置 mysql 服务,使用 systemctl 管理
vim /usr/lib/systemd/system/mysqld-3306.service
复制代码
内容如下:
[Unit]
Description=MySQL Server 3306
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0
# Execute pre and post scripts as root
PermissionsStartOnly=true
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/mysql-3306.cnf
LimitNOFILE = 5000
Restart=on-failure
RestartPreventExitStatus=1
复制代码
配置文件/etc/mysql-3306.cnf 内容如下:
[mysqld]
innodb_buffer_pool_size = 5000M
server_id=106
#log_slave_updates=1
port = 3306
datadir=/home/my3306
socket=/home/my3306/mysql.sock
log-error=/home/my3306/mysqld.log
pid-file=/home/my3306/mysqld.pid
log_bin=mysql-201-3306-binlog
# gtid最好是开启
gtid_mode=ON
enforce-gtid-consistency=ON
max_connections=2048
slow_query_log=ON
binlog_format=row
skip-name-resolve
log-slave-updates=1
relay_log_purge=0
back_log=128
wait_timeout=60
interactive_timeout=7200
key_buffer_size=16M
#query_cache_size=64M
#query_cache_type=1
#query_cache_limit=50M
max_connect_errors=20
sort_buffer_size=2M
max_allowed_packet=32M
join_buffer_size=2M
thread_cache_size=200
innodb_buffer_pool_size=1024M
innodb_flush_log_at_trx_commit=1
innodb_log_buffer_size=32M
innodb_log_file_size=128M
innodb_log_files_in_group=3
binlog_cache_size=2M
max_binlog_cache_size=8M
max_binlog_size=512M
expire_logs_days=7
read_buffer_size=2M
read_rnd_buffer_size=2M
复制代码
使用 systemctl 管理服务
systemctl daemon-reload
systemctl enable mysqld-3306.service
systemctl start mysqld-3306.service
systemctl stop mysqld-3306.service
systemctl restart mysqld-3306.service
复制代码
3. 添加用户
mysql 服务启动后,登录,默认密码是初始化的时候生成的那个密码
mysql -uroot -S /home/my3306/mysql.sock -p
# 需要先修改root用户的默认密码才能执行后面的所有操作
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Root@1234';
复制代码
新用户授权
use mysql;
create user user01@'172.16.0.%' identified by 'Root@1234';
grant all privileges on *.* to user01@'172.16.0.%' with grant option;
# 或者是
grant all privileges on *.* to 'user02'@'172.16.0.%' identified by 'Root@1234' with grant option;
grant all privileges on test.* to 'user03'@'172.16.0.%' identified by 'Root@1234' with grant option;
grant select on test.* to 'user04'@'172.16.0.%' identified by 'Root@1234' with grant option;
复制代码
根据需要进行授权即可
注意:mysql 5.7 默认的密码认证加密插件还是 mysql_native_password。
4. 其他问题
使用二进制代码安装,相对于 rpm 包安装服务配置要麻烦一些,不过单机多实例配置也相对灵活。可以在一个主机上设置多个不同版本的 mysql,只要在服务启动命令上做一些修改即可。
评论