Centos安装mysql
1、在/usr目录下新建mysql文件夹
mkdir mysql
上传下载的安装包(Xftp)

解压安装包
tar zxvf mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz
重命名解压的文件夹
mv mysql-5.7.44-linux-glibc2.12-x86_64/ mysql-5.7
创建用户组和用户
创建名为mysql的用户组
groupadd mysql
添加用户mysql到mysql用户组
useradd -r -g mysql mysql
配置MySQL
进入到刚才解压的MySQL目录下

创建一个名为data的目录
mkdir data

给用户组中的mysql用户赋予data目录的权限
chown mysql:mysql -R /usr/local/mysql/mysql-5.7/data/
配置my.cnf文件
vim /etc/my.cnf
[mysqld]
bind-address=0.0.0.0
port=3306
user=mysql
basedir=/usr/local/mysql/mysql-5.7
datadir=/usr/local/mysql/mysql-5.7/data
socket=/tmp/mysql.sock
log-error=/usr/local/mysql/mysql-5.7/data/mysql.err
pid-file=/usr/local/mysql/mysql-5.7/data/mysql.pid
max_connections=10000
max_user_connections=2000
wait_timeout=200
character_set_server=utf8mb4
symbolic-links=0
explicit_defaults_for_timestamp=true
lower_case_table_names=1
命令详解

按Esc键退出编辑模式,输入:wq保存并退出vim编辑器即可
初始化MySQL
先进入安装目录的bin目录下

初始化MySQL的数据目录
./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/mysql-5.7 --datadir=/usr/local/mysql/mysql-5.7/data --user=mysql --initialize

查看日志当中的初始密码
cat /usr/local/mysql/mysql-5.7/data/mysql.err

f(2(pppk0Dyj
启动MySQL
进入support-files目录下
cd /usr/local/mysql/mysql-5.7/support-files

运行mysql.server来启动MySQL服务
sudo ./mysql.server start

登录并修改初始密码
进入MySQL的bin目录
cd /usr/local/mysql/mysql-5.7/bin

登录MySQL
./mysql -u root -p
把刚才的f(2(pppk0Dyj
粘贴进来,然后敲回车

修改初始密码为root
SET PASSWORD = PASSWORD('root');

将root用户的密码过期策略设置为永不过期
ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;

刷新
FLUSH PRIVILEGES;

修改访问权限
use mysql

打开root用户的远程访问权限,允许任何主机使用root用户连接MySQL数据库
update user set host = '%' where user = 'root';

刷新
FLUSH PRIVILEGES;

设置开机自启
将MySQL的服务脚本创建一个符号链接到系统服务脚本目录下,文件名为mysql
ln -s /usr/local/mysql/mysql-5.7/support-files/mysql.server /etc/init.d/mysql


将MySQL的客户端程序mysql创建一个符号链接到系统命令目录/usr/bin下
ln -s /usr/local/mysql/mysql-5.7/bin/mysql /usr/bin/mysql

重启MySQL
service mysql restart

给/etc/init.d/mysql文件添加可执行权限
chmod +x /etc/init.d/mysql
将mysql服务添加为开机自动启动的服务
chkconfig --add mysql
查看服务列表
chkconfig --list
2-5级别显示为on,表示开机自动启动

PS:MySQL启动报错:Starting MySQL... ERROR! The server quit without updating PID file的问题分析和解决
[root@centos74 ~]# service mysqld start
Starting MySQL.. ERROR! The server quit without updating PID file (/usr/local/mysql/data/mysqld.pid).
可能已经存在 mysqld 进程.
ps -ef | grep mysqld
pkill -9 mysqld
补充:
关闭mysql服务
service mysql stop
启动mysql服务
service mysql start
重启mysql服务
service mysql restart