linux 下安装MySQL

在官网下载安装包

官网下载地址https://downloads.mysql.com/archives/community/
image

文档地址

https://dev.mysql.com/doc/refman/5.7/en/

在linux上安装

  • 上传下载的mysql文件
yum install -y lrzsz
rz 
  • 删除老版本的mysql
find / -name mysql
rm -rf 上边查找到的路径,多个路径用空格隔开
#或者下边一条命令即可
find / -name mysql|xargs rm -rf
  • 解压mysql tar包并改名
tar -xvf mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz 
mv mysql-5.7.33-linux-glibc2.12-x86_64 mysql
  • 添加mysql用户组和mysql用户
groupadd mysql
useradd -r -g mysql mysql
  • 进入mysql目录更改权限
cd mysql/
chown -R mysql:mysql ./
  • 执行安装脚本
./bin/mysqld --initialize --user=mysql --basedir=/data/mysql --datadir=/data/mysql/data
  • 配置文件
vim /etc/my.cnf
[mysqld]
basedir=/data/mysql # mysql安装目录
datadir=/data/mysql/data # mysql数据存放的目录
  • 拷贝启动文件
cp ./support-files/mysql.server /etc/init.d/
  • 启动
/etc/init.d/mysql.server start
  • 登录后设置密码
mysql -u root -p 
Enter password: 临时密码
set password=password("要设置的密码");
或者
alter user user() identified by "要设置的密码";

yum安装方式

  # 下载仓库
  wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
  # 配置仓库
  yum -y install mysql57-community-release-el7-10.noarch.rpm
  # 安装
  yum -y install mysql-community-server
  # 启动
  systemctl start  mysqld.service
  systemctl status mysqld.service
  # 查看默认密码
  grep "password" /var/log/mysqld.log
  # 登录
  mysql -uroot -p
  # 修改密码
  mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';
  SET PASSWORD = PASSWORD('password');
  # 其中‘new password’替换成你要设置的密码,注意:密码设置必须要大小写字母数字和特殊符号(,/';:等),不然不能配置成功
  # 远程访问
  grant all privileges on *.* to 'root'@'192.168.0.1' identified by 'password' with grant option;
  # 命令开启的IP是 192.168.0.1,如要开启所有的,用%代替IP
  # 刷新权限
  flush privileges; 
  # 开启防火墙
  firewall-cmd --zone=public --add-port=3306/tcp --permanent
  firewall-cmd --reload-
  # 修改字符编码
  vi /etc/my.cnf
  [client]
  default-character-set=utf8
  collation-server=utf8_general_ci
  character_set_server=utf8
  init_connect='SET NAMES utf8'
  # 重启查看字符编码
  systemctl restart mysqld.service
  mysql -u root -p 
  status
原文地址:https://www.cnblogs.com/huameixiao/p/14473680.html