Centos 解压安装MySQL

获取下载地址

https://mirrors.huaweicloud.com/mysql/Downloads/MySQL-8.0/

  • 全局页面搜索 Ctrl + F 右键复制链接

wget 粘贴链接

查看已有MySQL 服务

rpm -qa | grep mysql

卸载旧版MySQL

rpm -e --nodeps mysql-xxx

解压MySQL

tar -xvf mysql-8.0.22-el7-x86_64.tar -C /user/local 
mv mysql-8.0.22 mysql

创建数据目录及配置文件

vim /etc/my.cnf
[client]
port=3306
socket=/tmp/mysql/mysql.sock
[mysql]
default-character-set=utf8
[mysqld]
port=3306
user=mysql
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql
socket=/tmp/mysql/mysql.sock
max_connections=200
character-set-server=utf8
default-storage-engine=INNODB
lower_case_table_names=1
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
mkdir /usr/local/mysql/data

创建用户及用户组和创建临时目录文件

groupadd mysql
useradd -g mysql mysql
mkdir /tmp/mysql
chown -R mysql:mysql /tmp/mysql

初始化数据库

cd /usr/local/mysql/bin

初始化一个不带密码的root用户

./mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/

配置mysql环境变量

vim /etc/profile

在末尾追加一下内容

export MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/lib:$MYSQL_HOME/bin

重新加载配置文件

source /etc/profile

添加mysql服务及开机启动项

cp /usr/local/mysql/support-files/mysql.service /etc/init.d/mysql
chmod +x /etc/init.d/mysql
chkconfig --add mysql

可输入chkconfig --list查看启动项服务

管理mysql服务

  • 启动
service mysql start
  • 重启
service mysql restart
  • 停止
service mysql stop

登录mysql并设置远程访问

  • mysql -uroot -p # 没有密码直接登录

  • 使用mysql数据库 user mysql;

  • 修改root用户密码

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
  • 添加远程访问权限:
update user set host = '%' where user = 'root';
  • 刷新访问权限
FLUSH PRIVILEGES;
上一篇 下一篇