2022.07.02 CentOS8 上搭建Mysql 5.7
2022-07-02 本文已影响0人
薛定谔的猴子
一、资源下载
下载 : mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
二、安装
1、解压:
tar -xzvf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
2、创建mysql 软件要存放的文件夹
mkdir /usr/local/mysql
3、移动解压文件到文件夹,并删除空文件夹
cd /usr/local
mv mysql-5.7.37-linux-glibc2.12-x86_64/* /usr/local/mysql
rmdir mysql-5.7.37-linux-glibc2.12-x86_64
4、创建数据目录
mkdir /usr/local/mysql/data
5、查看组和用户情况
cat /etc/group | grep mysql
cat /etc/passwd |grep mysql
若存在,则删除原mysql用户,会删除其对应的组和用户:
userdel -r mysql
创建mysql组和mysql用户:
groupadd mysql
useradd -r -g mysql mysql
授权:
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /var/log
6、修改配置文件:/etc/my.cnf,配置不对的话,后面初始化不全,会拿不到默认密码
vim /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
socket=/tmp/mysql.sock
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/tmp/mysqld/mysqld.pid
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
log-bin=mysql-bin
binlog-format=ROW
server_id=1
max_connections=1000
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
explicit_defaults_for_timestamp=true
7、添加 mysql.sock 和 mysqld.pid
cd /tmp
touch mysql.sock
chown mysql:mysql mysql.sock
chmod 755 mysql.sock
创建文件/tmp/mysqld/mysqld.pid:
mkdir mysqld
cd mysqld
touch mysqld.pid
cd ..
chown -R mysql:mysql mysqld
cd mysqld
chmod 755 mysqld.pid
8、初始化数据库
cd /usr/local/mysql/bin
./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
安全启动:
./mysqld_safe --user=mysql &
默认密码在mysqld.log日志里, 找到后保存到安全的地方:
cat /var/log/mysqld.log
查找下面的语句:!0dt6dXvZb5q 是密码
A temporary password is generated for root@localhost: !0dt6dXvZb5q
9、登陆数据并更改密码:
cd /usr/local/mysql/bin/
./mysql -u root -p
mysql登录报错:mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
执行下面命令:
yum install libncurses*
更改默认密码
set password=password("xsd2018");
设置远程登录权限
grant all privileges on *.* to 'root'@'%' identified by 'xsd2018';
立即生效:
flush privileges;
10、设置开机启动
cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
查看mysql服务是否在服务配置中
chkconfig --list mysql
若没有,则把mysql注册为开机启动的服务,然后在进行查看
chkconfig --add mysql
chkconfig --list mysql
启动 或 停止
service mysql start
service mysql stop
11、设置或者关闭防火墙
- 查看防火状态
systemctl status firewalld
service iptables status
- 暂时关闭防火墙
systemctl stop firewalld
service iptables stop
- 永久关闭防火墙
systemctl disable firewalld
chkconfig iptables off
- 重启防火墙
systemctl enable firewalld
service iptables restart
- 永久关闭后重启
chkconfig iptables on