CentOS7安装MySQL5.7

2022-07-15  本文已影响0人  A二十一画

[toc]

MySQL安装部署

系统:CentOS7
版本:MySQL5.7

RPM/YUM安装MySQL

1、下载MySQL的yum仓库文件包并安装MySQL的yum源

wget http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql57-community-release-el7-10.noarch.rpm
rpm -ivh mysql57-community-release-el7-10.noarch.rpm

2、Yum安装MySQL并启动

yum -y install mysql-server
systemctl start mysqld

3、设置密码

cat /var/log/mysqld.log | grep password
...
2022-04-25T02:43:47.618549Z 1 [Note] A temporary password is generated for root@localhost: IYRlP&Uor8eK
...
mysql -uroot -p -> IYRlP&Uor8eK

# 修改的密码要符合密码复杂度
mysql> alter user 'root'@'localhost' identified by 'Abc123./';

遇到问题:
1、执行第2步安装时遇到以下报错

...
mysql-community-libs-compat-5.7.37-1.el7.x86_64.rpm 的公钥尚未安装

失败的软件包是:mysql-community-libs-compat-5.7.37-1.el7.x86_64 
GPG  密钥配置为:file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
...

解决办法:
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
然后重新安装

二进制安装MySQL

1、清理环境

# 查看是否有rpm包(若有则卸载)
rpm -qa | grep mysql

# 卸载CentOS7系统自带的mariaDB
rpm -qa|grep mariadb
    mariadb-libs-5.5.44-2.el7.centos.x86_64
# 卸载系统自带的Mariadb
rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64
# 删除etc目录下的my.cnf ,一定要删掉,等下再重新建,之前我将就用这个文件,后面改配置各种不生效
rm /etc/my.cnf

2、创建MySQL用户组

groupadd mysql
useradd -r -g mysql mysql -s /sbin/nologin

3、下载官网的Tar.gz包,或者下载之后上传到服务器并解压

# 下载tar包
wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.35-el7-x86_64.tar.gz

# 解包
tar -zxvf mysql-5.7.35-el7-x86_64.tar.gz -O /usr/local/services
cd !$
mv mysql-5.7.35-el7-x86_64 mysql5.7

4、创建相关目录并更改用户组权限

# 数据目录
mkdir /mnt/my-data
chown -R mysql:mysql /mnt/my-data

# 日志目录
mkdir -p /var/log/mysql/logs
chown -R mysql:mysql /var/log/mysql/logs

# 存放sock、pid目录
mkdir /usr/local/services/mysql5.7/tmp
chown -R mysql:mysql /usr/local/services/mysql5.7

5、配置my.cnf文件

[client]
port = 3306
socket = /usr/local/services/mysql5.7/tmp/mysql.sock

[mysqld]
user = mysql
basedir = /usr/local/services/mysql5.7
datadir = /mnt/my-data
port = 3306
socket = /usr/local/services/mysql5.7/tmp/mysql.sock
pid-file = /usr/local/services/mysql5.7/tmp/mysqld.pid
tmpdir = /usr/local/services/mysql5.7/tmp
skip_name_resolve = 1
symbolic-links=0
max_connections = 2000
group_concat_max_len = 1024000
sql_mode = NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
lower_case_table_names = 1
log_timestamps=SYSTEM
character-set-server = utf8
interactive_timeout = 1800
wait_timeout = 1800
max_allowed_packet = 32M
binlog_cache_size = 4M
sort_buffer_size = 2M
read_buffer_size = 4M
join_buffer_size = 4M
tmp_table_size = 96M
max_heap_table_size = 96M
max_length_for_sort_data = 8096
# skip-grant-tables=1

#logs
server-id = 1003306
log-error = /var/log/mysql/logs/error.log
slow_query_log = 1
slow_query_log_file = /var/log/mysql/logs/slow.log
long_query_time = 3
log-bin = /var/log/mysql/logs/binlog
binlog_format = row
expire_logs_days = 15
log_bin_trust_function_creators = 1
relay-log = /var/log/mysql/logs/relay-bin
relay-log-recovery = 1
relay_log_purge = 1

#innodb
innodb_file_per_table = 1
innodb_log_buffer_size = 16M
innodb_log_file_size = 256M
innodb_log_files_in_group = 2
innodb_io_capacity = 2000
innodb_io_capacity_max = 4000
innodb_flush_neighbors = 0
innodb_flush_method = O_DIRECT
innodb_autoinc_lock_mode = 2
innodb_read_io_threads = 8
innodb_write_io_threads = 8
innodb_buffer_pool_size = 2G

6、配置系统环境

# 设置chkconfig
cd /usr/local/mysql/support-files
cp mysql.server /etc/init.d/mysqld

# 修改目录位置
vim /etc/init.d/mysqld
...
basedir=/usr/local/services/mysql5.7
datadir=/mnt/my-data
...

# 注册开机自启
chkconfig --add mysqld
chkconfig --list

# 添加环境变量
vim /etc/profile
    export PATH=$PATH:/usr/local/services/mysql5.7/bin
source /etc/profile

# 设置软链
ln -s /usr/local/services/mysql5.7/bin/mysql /usr/bin/mysql

7、初始化

/usr/local/services/mysql5.7/bin/mysqld --initialize --user=mysql --basedir=/usr/local/services/mysql5.7 --datadir=/mnt/my-data

# 查找密码
more /var/log/mysql/logs/error.log | grep password

8、登录并修改密码

# 密码是上个步骤grep出的结果
mysql -uroot -p 

# 修改密码
mysql> alter user 'root'@'localhost' identified by 'rootroot';
mysql> flush privileges;
上一篇下一篇

猜你喜欢

热点阅读