1.安装环境
1.1操作系统
CentOS Linux release 7.7.1908 (Core)
1.2数据库版本
5.7.29
2.下载MySQL安装文件
2.1官网下载地址
https://dev.mysql.com/downloads/mysql/5.7.html#downloads
2.2选择对应的MySQL数据库文件下载
2.3官方文档
https://dev.mysql.com/doc/refman/5.7/en/binary-installation.html
3.上传文件到安装目录
3.1将安装文件上传到Linux指定目录
[root@localhost src]# ll -h /usr/local/src/
total 634M
-rw-r--r--. 1 root root 634M Mar 14 19:06 mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz
3.2解压文件
tar -zxvf mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz
3.3复制到软件安装目录并重命名
cp -r mysql-5.7.29-linux-glibc2.12-x86_64 /usr/local/mysql
[root@localhost src]# ll /usr/local/mysql/
total 288
drwxr-xr-x. 2 root root 4096 Mar 14 20:10 bin
drwxr-xr-x. 2 root root 55 Mar 14 20:10 docs
drwxr-xr-x. 3 root root 4096 Mar 14 20:10 include
drwxr-xr-x. 5 root root 230 Mar 14 20:10 lib
-rw-r--r--. 1 root root 276202 Mar 14 20:10 LICENSE
drwxr-xr-x. 4 root root 30 Mar 14 20:10 man
-rw-r--r--. 1 root root 587 Mar 14 20:10 README
drwxr-xr-x. 28 root root 4096 Mar 14 20:10 share
drwxr-xr-x. 2 root root 90 Mar 14 20:10 support-files
4.设置MySQL目录权限
4.1新建组
groupadd mysql
4.2新建用户
useradd -r -g mysql mysql
4.3修改MySQL目录权限
chown -R mysql:mysql /usr/local/mysql/
5.配置MySQL服务
5.1复制mysql/support-files下的mysql.server
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
5.2修改mysqld服务
vim /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
5.3配置my.cnf配置文件
vim /etc/my.cnf
[mysqld]
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[client]
port = 3306
default-character-set=utf8
[mysqld]
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character-set-server=utf8
default_storage_engine = InnoDB
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
6.初始化数据库
6.1进入mysql/bin目录
cd /usr/local/mysql/bin/
6.2初始化数据库
./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
6.3执行完毕,注意保存生成的默认密码
7.环境变量配置
7.1 /etc/profile末尾添加
vim /etc/profile
# mysql environment
export MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin
7.2更新环境变量
source /etc/profile
8.启动服务
8.1执行启动命令
service mysqld start
8.2设置开机自启
8.2.1添加MySQl服务到自启服务
chkconfig --add mysqld
8.2.2设置自启
chkconfig mysqld on
8.2.3检查自启服务
chkconfig --list |grep mysqld
9.登录及配置
9.1登录
mysql -uroot -p
密码为初始化数据库后,控制台打印的临时密码
9.2修改密码
set password=password('新密码');
9.3设置远程访问
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '访问密码';
flush privileges;