CentOS 7.9使用yum安装MySQL 5.7
- 下载MySQL的yum源:
wget http://repo.mysql.com/mysql57-community-release-el7-11.noarch.rpm
- 安装MySQL的yum源:
rpm -Uvh mysql57-community-release-el7-11.noarch.rpm
- 查看MySQL的yum源:
yum repolist all | grep mysql
- 设置MySQL安装版本:
yum-config-manager --enable mysql57-community
yum-config-manager --disable mysql80-community
- 安装MySQL:
yum install mysql-community-server
- 设置MySQL数据目录和socket等:
vim /etc/my.cnf
1
2
3
4
5
6datadir=/opt/mnt/mysqldata/mysql/mysql
socket=/opt/mnt/mysqldata/mysql/mysql/mysql.sock
innodb_log_file_size=1024M
innodb_strict_mode=0
[client]
socket=/opt/mnt/mysqldata/mysql/mysql/mysql.sock #不配置client的socket登录MySQL出现"ERROR 2002 (HY000): Can’t connect to local MySQL server through socket '/tmp/mysql.sock' (2)"的错误 - MySQL启动前设置:
chown -R mysql:mysql /opt/mnt/mysql/mysql
setenforce 0
(临时关闭selinux)chcon -R --reference=/var/lib/mysql /opt/mysqldata
(出现错误SELinux is preventing /usr/sbin/mysqld from write access on the directory mysql)
- MySQL服务的操作:
- 启动:
systemctl start mysqld
- 关闭:
systemctl stop mysqld
- 重启:
systemctl restart mysqld
- 状态:
systemctl status mysqld
- 开机启动:
systemctl enable mysqld
- 启动:
- 授权root可以远程登录访问:
- MySQL初始化的临时密码:
grep 'temporary password' /var/log/mysqld.log
(若MySQL已有密码则无法得到临时密码) - 登录MySQL:
mysql -u root -p
- 修改默认密码: mysql>
ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';
- 授权root可以远程登录访问:
mysql>GRANT ALL PRIVILEGES ON * . * TO 'root'@'%' IDENTIFIED BY 'password';
mysql>flush privileges;
(刷新权限) - 退出MySQl: mysql>
quit
- MySQL初始化的临时密码:
- 防火墙开放3306端口:
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload
(重启防火墙)
开放3306端口数据库连接工具才可以连接到数据库
*查看Linux的版本: less /etc/redhat-release
(只能用于Redhat系的Linux)
*MySQL的yum安装参考资料: https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/
CentOS 7.9安装MySQL 8.0.28
- 下载MySQL: https://dev.mysql.com/downloads/mysql/
- 解压:
tar xvJf mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz
- 创建数据目录:
mv mysql-8.0.28-linux-glibc2.12-x86_64 mysql
cd mysql
mkdir data
- 创建用户组,用户并赋予权限:
groupadd mysql
useradd -g mysql mysql
chown -R mysql.mysql /opt/mysql
- MySQL加入环境变量:
vim /etc/profile
export PATH=$PATH:/opt/mysql/bin:/opt/mysql/lib
- 配置my.cnf:
mkdir /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql
vim /etc/my.cnf
1
2
3
4
5
6
7
8
9[mysqld]
basedir=/opt/mysql
datadir=/opt/mysql/data
socket=/var/lib/mysql/mysql.sock
[client]
socket=/var/lib/mysql/mysql.sock
[mysqld_safe]
log-error=/opt/mysql/data/mysql.log
pid-file=/opt/mysql/data/mysql.pid - MySQL初始化:
/opt/mysql/bin/mysqld --user=mysql --basedir=/opt/mysql --datadir=/opt/mysql/data --initialize
(记住临时密码) - 设置MySQL为系统服务并启动:
cp -a /opt/mysql/support-files/mysql.server /etc/init.d/mysql
chkconfig --add mysql
systemctl start mysql
- 设置root远程登录:
mysql -u root -p
(用临时密码登录)1
2
3
4
5
6alter user 'root'@'localhost' identified with mysql_native_password by 'password';
flush privileges;
use mysql;
update user set host='%' where user='root';
flush privileges;
quit
- 防火墙开启3306端口:
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload