下载mysql安装包
https://downloads.mysql.com/archives/community/
我下的是
mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz
查询系统版本
# uname -a
上传安装包到服务器上
# cd /usr/local
# mkdir mysql
# cd mysql
# rz mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz
解压安装包
# tar -zxvf mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz
重命名mysql解压的文件夹
# mv mysql-5.7.32-linux-glibc2.12-x86_64 mysql-5.7.32
修改/etc/my.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
port = 3306
socket = /tmp/mysql.sock
basedir = /usr/local/mysql/mysql-5.7.32
datadir = /usr/local/mysql/mysql-5.7.32/data
log-error = /usr/local/mysql/mysql-5.7.32/mysqld.log
pid-file = /usr/local/mysql/mysql-5.7.32/mysqld.pid
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
lower_case_table_names = 1 # 不区分大小写
sql_mode = 'STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO'
default-time_zone = '+8:00'
创建用户组
groupadd mysql #创建用户组
-r 参数表示 mysql 用户是系统用户,不可用于登录系统,创建用户 mysql 并将其添加
到用户组 mysql 中
useradd -r -g mysql mysql
分配用户组:
chown -R mysql /usr/local/mysql/mysql-5.7.32# 将文件的所有属性改为 mysql 用户
chgrp -R mysql /usr/local/mysql/mysql-5.7.32# 将组属性改为 mysql 组
初始化数据库
# cd /usr/local/mysql/mysql-5.7.32/bin
# ./mysqld --initialize --user=mysql
# cat /usr/local/mysql/mysql-5.7.32/mysqld.log
root的初始密码在mysqld.log已生成
启动服务
# cd /usr/local/mysql/mysql-5.7.32/support-files
# ./mysql.server start
启动后登录
登录前执行,否则报错:
sudo ln -s /usr/lib64/libtinfo.so.6.1 /usr/lib64/libtinfo.so.5
[root@localhost bin]# ./mysql -u root -p
修改密码:
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
加入系统服务
# 复制 mysql.server 至/etc/init.d/重命名为 mysqld
cp /usr/local/mysql/mysql-5.7.32/support-files/mysql.server /etc/init.d/mysqld
# 修改 vim /etc/init.d/mysqld 配置文件
修改内容:
basedir=/usr/local/mysql/mysql-5.7.32
datadir=/usr/local/mysql/mysql-5.7.32/data
# 添加系统服务(service)启动信息
chkconfig --add mysqld # 服务名字为mysqld
启动服务并查状态:
systemctl start mysqld
systemctl status mysqld
设置环境变量
# vi /etc/profile
修改编辑vi /etc/profile文件,与配置jdk类同,添加如下:
export MYSQL_HOME=/usr/local/mysql/mysql-5.7.32/
export PATH=$PATH:$JAVA_HOME/bin:$MYSQL_HOME/bin
# source /etc/profile
授权,远程访问
进入mysql
# mysql -u root -p
修改访问范围
# 改表法
# 选择数据库
use mysql;
# 最后授权 MySQL,允许远程用户登录访问 MySQL
update user set host = '%' where user = 'root';
# 刷新权限
flush privileges;
解决SQLyog连接MySQL时出现的2058错误解决方法
mysql -u root -p登录
通过命令 select user,plugin from user where user='root'; 我们可以发现加密方式是caching_sha2_password
修改加密方式alter user 'root'@'%' identified with mysql_native_password by '123456';
找到my.cnf加上default_authentication_plugin=mysql_native_password
保存后,重启mysql服务命令:mysql.server restart