1、压缩包获取
1.1 通过wget命令从官网下载
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
1.2 window下载然后上传到linux服务器
1.3 百度网盘下载
链接::https://pan.baidu.com/s/1LtbGI8yKXok4aQrBxq6gYA
提取码: cgha
2、准备文件和目录
2.1 将压缩文件上传至“/usr/local”目录下
2.2 解压文件:tar -zxvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
2.3 文件重命名:mv mysql-5.7.22-linux-glibc2.12-x86_64 mysql
2.4 准备文件和文件夹:
进入mysql目录执行以下命令:
2.4.1 mkdir data
2.4.2 mkdir logs
2.4.3 mkdir pids
分别进入logs和pids,分别执行touch mysqld.log和touch mysqld.pid
3、创建用户和用户组
3.1 创建用户组
groupadd mysql
3.2 创建用户
useradd mysql -g mysql
3.3 将/usr/local/mysql的所有者及所属组改为mysql
chown -R mysql.mysql /usr/local/mysql
4、初始化数据库
使用命令(建议直接使用该命令,方便后续修改密码):
/usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize
如果执行该命令提示:
libraries:****.so: cannot open shared object file: No such file or directory
则使用:
yum -y install ***
安装,然后再执行以上命令
5、配置mysql配置文件
配置文件:/etc/my.cnf
[mysqld]
default_password_lifetime=0
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/mysql.sock
[mysqld_safe]
log-error=/usr/local/mysql/logs/mysqld.log
pid-file=/usr/local/mysql/pids/mysqld.pid
# 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
[client]
default-character-set=utf8
socket=/usr/local/mysql/mysql.sock
[mysql]
default-character-set=utf8
socket=/usr/local/mysql/mysql.sock
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
6、设置服务
6.1 添加mysql服务
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
6.2 设置开机启动
chkconfig mysql on
6.3 开启mysql
service mysql start
如果提示:
my_print_defaults: Can’t read dir of ‘/etc/my.cnf.d’ (Errcode: 2 - No such file or directory)
my_print_defaults: [ERROR] Fatal error in defaults handling. Program aborted!
Starting MySQL. ERROR! The server quit without updating PID file (/usr/local/mysql/data/ip.pid).
创建目录:
mkdir /etc/my.cnf.d/
如果启动提示:
Starting MySQL. ERROR! The server quit without updating PID file (/usr/local/mysql/data/host.pid)
在/usr/local下执行:
chown -R mysql:mysql ./
7、修改数据库配置
7.1 修改密码
使用命令:
/usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize
的日志中,会出现密码。
登录mysql
./mysql -uroot -p'Chywt+6WlIE>'
如果密码有特殊字符,可以用引号
登录成功后:
SET PASSWORD = PASSWORD('123456');
或
ALTER USER USER() IDENTIFIED BY '123456';
“123456”是修改后的密码
7.2 修改权限
将所有数据库所有表的权限赋给用户root,允许所有主机访问
grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
刷新权限设置:
flush privileges;
退出
exit
建立软链接:
ln -s /usr/local/mysql/bin/mysql /usr/bin
如果提示已经建立,可先删除,不然无法生效
建立软链接之后,可以在其他目录下使用登录命令直接登录:
mysql -uroot -p123456