MySql最新版本下载
MySql老版本检查
我们在Linux系统中,如果要使用关系型数据库的话,基本都是用的MySql,而且以往7以下版本的centos系统都是默认的集成有MySql,然而对于现在最新的centos7系统来说,已经不支持mysql数据库,它默认内部集成了mariaDB,如果我们想要使用 mysql 的话,就要先将原来的mariaDB卸载掉,不然会引起冲突。
检查是否已安装老版本MySql
rpm -qa | grep -I mysql
如果输出,类似
mysql-libs-xxx.x86\_64\
则先关闭mysql服务,再卸载老版本MySql
sudo rpm -e --nodeps mysql-libs-5.1.52.x86_64
检查是否已安装老版本mariadb
rpm -qa | grep mariadb
如果输出,类似
mariadb-libs-5.5.52-1.el7.x86_64
则先卸载老版本mariadb
sudo rpm -e mariadb-libs-5.5.52-1.el7.x86_64
注意:
- 卸载后/etc/my.cnf不会删除,需要进行手工删除
- 有可能mariadb要某些依赖才能卸载,安装所需依赖后再卸载,或者直接强制卸载
# 缺少依赖
# libmysqlclient.so.18()(64bit) 被 (已安裝) postfix-2:2.10.1-6.el7.x86_64 需要
# libmysqlclient.so.18(libmysqlclient_18)(64bit) 被 (已安裝) postfix-2:2.10.1-6.el7.x86_64 需要
# 强制卸载
sudorpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64
检查操作系统
cat /etc/redhat-release
输出结果
CentOS Linux release 7.3.1611 (Core)
Msql安装
下载
wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.21-linux-glibc2.17-x86_64-minimal.tar.xz
解压
.tar.xz这种文件其实是两层压缩,外层是.xz压缩方式,内层是.tar压缩方式
解压过程可以分两步操作:
# 先解压外层,得到 mysql-8.0.21-linux-glibc2.17-x86_64-minimal.tar
xz -d mysql-8.0.21-linux-glibc2.17-x86_64-minimal.tar.xz
# 再解压内层
tar xvf mysql-8.0.21-linux-glibc2.17-x86_64-minimal.tar -C /usr/local/
也可以
tar xf mysql-8.0.21-linux-glibc2.17-x86_64-minimal.tar.xz -C /usr/local/
修改MySql解压后文件夹名称
mv /usr/local/mysql-8.0.21-linux-glibc2.17-x86_64-minimal /usr/local/mysql-8.0.21
修改环境变量
vim /etc/profile
export PATH=/usr/local/mysql-8.0.21/bin:$PATH
刷新环境变量
source /etc/profile
创建mysql用户和组
groupadd mysql
useradd -r -g mysql mysql
创建相关目录并修改权限
mkdir /var/mysql/data -p
chown -R mysql.mysql /usr/local/*
chown -R mysql.mysql /var/mysql/*
初始化MySql数据库
cd /usr/local/mysql-8.0.21/bin
# 方法一:自动生成数据库密码
./mysqld --initialize --user=mysql --basedir=/usr/local/mysql-8.0.21 --datadir=/var/mysql/data
# 方法二:不生成密码,设置空密码
./mysqld --initialize -insecure --user=mysql --basedir=/usr/local/mysql-8.0.21 --datadir=/var/mysql/data
若自动生成数据库密码,则记下生成的密码
[Note] [MY-010454] [Server] A temporary password is generated for root@localhost: AQjmET,w+2Nl
添加默认配置文件
vim /etc/my.cnf
[mysqld]
user=mysql
basedir=/usr/local/mysql-8.0.21
datadir=/var/mysql/data
server_id=6
port=3306
socket=/tmp/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysql]
socket=/tmp/mysql.sock
[mysqld_safe]
log-error=/var/log/mysql/error.log
pid-file=/var/run/mysql/mysql.pid
[client]
port=3306
socket=/tmp/mysql.sock
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
根据my.cnf文件路径配置,创建mysql目录下对应的目录和文件
mkdir /etc/my.cnf.d
# 创建log目录
mkdir /var/log/mysql
# 创建log文件
cd /var/log/mysql
touch error.log
# 授权log文件
chown -R mysql.mysql /var/log/mysql/error.log
mkdir /var/run/mysql
chown -R mysql.mysql /var/run/mysql/*
启动MySql
cd /usr/local/mysql-8.0.21/support-files
./mysql.server start
停止MySql
./mysql.server stop
将/usr/local/mysql-8.0.21/support-files/mysql.server复制到目录/etc/init.d/mysqld
cp /usr/local/mysql-8.0.21/support-files/mysql.server /etc/init.d/mysqld
MySql启动/停止
service mysqld start/stop/restart/status
添加mysqld系统服务脚本
创建并打开/usr/lib/systemd/system/mysqld.service文件
/usr/lib/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
ExecStart=/usr/local/mysql-8.0.21/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
# Restart=on-failure
# RestartPreventExitStatus=1
# PrivateTmp=false
扩展阅读
设置开机启动:
systemctl enable mysqld
取消开机启动:
systemctl disable mysqld
查看开机启动状态:
systemctl is-enabled mysqld
启动服务:
systemctl start mysqld
停止服务:
systemctl stop mysqld
重启服务:
systemctl restart mysqld
查看服务状态:
systemctl status mysqld
重新加载某个服务的配置文件:
systemctl reload xxx.service
重载所有修改过的配置文件:
sudo systemctl daemon-reload
扩展阅读
登录mysql
mysql -uroot -p
修改密码
# MySQL版本5.7.6版本以前用户可以使用如下命令:
SET PASSWORD = PASSWORD('test123456');
# MySQL版本5.7.6版本开始的用户可以使用如下命令:
ALTER USER USER() IDENTIFIED BY 'test123456';
开启Mysql远程访问
方法一:直接改表
select host,user from user;
update user set host = '%' where user = 'root';
方法二:授权
# 你想root使用root从任何主机连接到mysql服务器的话。
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
# 如果你想允许用户myuser从ip为192.168.1.64的主机连接到mysql服务器,并使用root作为密码
GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.64' IDENTIFIED BY
'root' WITH GRANT OPTION;
FLUSH PRIVILEGES;
参考资料:
Linux Centos7 下安装Mysql - 8.0.15
解决Navicat for MySQL 连接 Mysql 8.0.11 出现1251- Client does not support authentication protocol 错误