部署(rpm形式)
一、下载
MySQL :: Download MySQL Community Server (Archived Versions) 注意,由于是Centos 7 ,就下载Linux 7 ,选择 Bundle
二、部署
[root@jerry src]# ll
-rw-r--r--. 1 root root 906106880 1月 3 15:44 mysql-8.0.31-1.el7.x86_64.rpm-bundle.tar
[root@jerry src]# tar -xvf mysql-8.0.31-1.el7.x86_64.rpm-bundle.tar
[root@jerry src]# ls
mysql-8.0.31-1.el7.x86_64.rpm-bundle.tar mysql-community-icu-data-files-8.0.31-1.el7.x86_64.rpm
mysql-community-client-8.0.31-1.el7.x86_64.rpm mysql-community-libs-8.0.31-1.el7.x86_64.rpm
mysql-community-client-plugins-8.0.31-1.el7.x86_64.rpm mysql-community-libs-compat-8.0.31-1.el7.x86_64.rpm
mysql-community-common-8.0.31-1.el7.x86_64.rpm mysql-community-server-8.0.31-1.el7.x86_64.rpm
mysql-community-debuginfo-8.0.31-1.el7.x86_64.rpm mysql-community-server-debug-8.0.31-1.el7.x86_64.rpm
mysql-community-devel-8.0.31-1.el7.x86_64.rpm mysql-community-test-8.0.31-1.el7.x86_64.rpm
mysql-community-embedded-compat-8.0.31-1.el7.x86_64.rpm
部署顺序不能乱,否则可能会提示缺少对应的依赖
mysql-community-common-8.0.31-1.el7.x86_64.rpm
mysql-community-client-plugins-8.0.31-1.el7.x86_64.rpm
mysql-community-libs-8.0.31-1.el7.x86_64.rpm
mysql-community-client-8.0.31-1.el7.x86_64.rpm
mysql-community-icu-data-files-8.0.31-1.el7.x86_64.rpm
mysql-community-server-8.0.31-1.el7.x86_64.rpm
安装 MySQL
# 由于 MySQL 安装过程中,会通过 MySQL 用户在 /tmp 目录下新建 tmp_db 文件。赋予其权限执行:
chmod -R 777 /tmp
# 检查依赖是否存在(CentOS7安装了桌面环境默认存在)
rpm -qa|grep libaio
rpm -qa|grep net-tools
# MySQL 与 mysql-libs 冲突,提前移除
yum remove mysql-libs 或者 rpm -e mariadb-libs --nodeps
# 执行安装(严格按照顺序)
rpm -ivh mysql-community-common-8.0.31-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-plugins-8.0.31-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.31-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-8.0.31-1.el7.x86_64.rpm
rpm -ivh mysql-community-icu-data-files-8.0.31-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-8.0.31-1.el7.x86_64.rpm
部署过程遇缺少.so的依赖
[root@jerry src]# rpm -ivh mysql-community-libs-8.0.31-1.el7.x86_64.rpm
警告:mysql-community-libs-8.0.31-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
错误:依赖检测失败:
mariadb-libs 被 mysql-community-libs-8.0.31-1.el7.x86_64 取代
[root@jerry src]# rpm -e mariadb-libs --nodeps # 卸载mariadb-libs
[root@jerry src]# rpm -ivh mysql-community-server-8.0.31-1.el7.x86_64.rpm
警告:mysql-community-server-8.0.31-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
错误:依赖检测失败:
net-tools 被 mysql-community-server-8.0.31-1.el7.x86_64 需要
[root@jerry src]# yum install -y net-tools
简单配置
# 查看是否安装成功
mysql --version
# root用户下进行初始化
mysqld --initialize --user=mysql
# 查看初始密码
cat /var/log/mysqld.log
# 启动 MySQL
systemctl start mysqld
# 查看状态
systemctl status mysqld
# 查看 MySQL 自启动状态
systemctl list-unit-files|grep mysqld.service
# enable 为自启动打开,disable 为自启动关闭
# systemctl enable mysqld(将某服务设置为自启动状态)
# systemctl disable mysqld(将某服务设置为自启动状态)# 登录 MySQL
mysql -uroot -p
# 修改初始密码(root'@'localhost),如果不修改的密码,其他操作包括修改root'@'%,一切命令都不好使ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
# 刷新配置文件立即生效
flush privileges;
远程连接 Linux 主机的 MySQL
# 关闭本机及远程主机防火墙或开放端口号(本文采用关闭防火墙)
# Linux 下防火墙设置
# 查看防火墙状态
systemctl status firewalld
# 关闭防火墙
systemctl stop firewalld
# 关闭防火墙开机自启动
systemctl disable firewalld
# 启用 MySQl 远程登录权限
方式一:将'root'@'localhost'修改成root'@'%,不推荐
# 切换数据库
use mysql;
# 查看登录权限
select Host,User from user;
# 修改登录权限
update user set host = '%' where user ='root'; # 工作中尽量更严格使用通配符
# 刷新配置文件立即生效
flush privileges;
# 若报错"2058",执行
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'new_password';
方式二:创建一个新的root'@'%
# 默认mysql不带root'@'%的账户
mysql> select host,user from mysql.user;
+-----------+------------------+
| host | user |
+-----------+------------------+
| localhost | mysql.infoschema |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+------------------+
4 rows in set (0.01 sec)
mysql> create user 'root'@'%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)
mysql> grant all privileges on *.* to 'root'@'%' with grant option;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> select host,user from mysql.user;
+-----------+------------------+
| host | user |
+-----------+------------------+
| % | root |
| localhost | mysql.infoschema |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+------------------+
5 rows in set (0.00 sec)
mysql>
修改字符集(MySQL 5.7版本)
# 查看默认字符集
show variables like '%char%';
# 修改字符集,Windows下配置文件为 my.ini ,Linux 下配置文件为 /etc/my.cnf
vim /etc/my.cnf
# 将以下文本追加到最后
character_set_server=utf8
# 重启 MySQL 服务
systemctl restart mysqld
# 原库、原表的设定不会发生变化,参数修改只对新建的数据库生效
# 修改已创建数据库的字符集
alter database dbtest1 character set 'utf8';
# 修改已创建数据表的字符集
alter table t_emp convert to character set 'utf8';
部署(tar形式)
一、下载
MySQL :: Download MySQL Community Server (Archived Versions) 选择版本,选择Linux,选择架构
选择这个TAR
二、部署
tar -xvf mysql-8.0.23-el7-x86_64.tar
mv mysql-8.0.23-el7-x86_64 /usr/local/mysql-8.0.23
cd /usr/local/
ln -s mysql-8.0.23/ mysql
groupadd mysql
useradd -r -g mysql mysql
mkdir -p /data/mysql
chown -R mysql:mysql /usr/local/mysql-8.0.23
chown -R mysql:mysql /data/mysql
vim /etc/my.cnf # 配置 my.cnf (基础)
[mysqld]
bind-address=0.0.0.0
port=3306
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/usr/local/mysql/mysql.sock
log-error=/data/mysql/mysql.err
pid-file=/data/mysql/mysql.pid
#character config
character_set_server=utf8mb4
symbolic-links=0
explicit_defaults_for_timestamp=true
[mysql]
socket=/usr/local/mysql/mysql.sock
[client]
socket=/usr/local/mysql/mysql.sock
初始化数据库
cd /usr/local/mysql/bin
./bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql --initialize
查看mysql 初始化密码(随机生成的)
cat /data/mysql/mysql.err
启动mysql (systemctl)
vim /etc/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
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
Type=forking
PIDFile=/data/mysql/mysql.pid
TimeoutSec=0
PermissionsStartOnly=true
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --pid-file=/data/mysql/mysql.pid --daemonize
LimitNOFILE = 65535
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=false
提权:chmod a+x /etc/systemd/system/mysqld.service
启动Mysql :systemctl start mysqld.service
登录 MySQL
mysql -uroot -p
修改初始密码(root'@'localhost),如果不修改的密码,其他操作包括修改root'@'%,一切命令都不好使
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
刷新配置文件立即生效
flush privileges;