环境:
1、操作系统:CentOS release 6.8 (Final)
2、安装版本: mysql-5.6.31-linux-glibc2.5-x86_64.tar.gz
3、下载地址:http://dev.mysql.com/downloads/mysql/
4、下载说明:上边的下载地址是最新版的,如果想下载老版本可以点击页面中的超链接“Looking for previous GA versions?”
5、特殊说明:Linux下MySQL5.6与MySQL5.7安装方法略有不同,本文以5.6为主
安装步骤
0、卸载老版本MySQL
查找并删除mysql有关的文件
find / -name mysql rm -rf 上边查找到的路径,多个路径用空格隔开 #或者下边一条命令即可 find / -name mysql|xargs rm -rf

1、在安装包存放目录下执行命令解压文件:
tar -zxvf mysql-5.6.31-linux-glibc2.5-x86_64.tar.gz

2、删除安装包,重命名解压后的文件
rm -f mysql-5.6.31-linux-glibc2.5-x86_64.tar.gz mv mysql-5.6.31-linux-glibc2.5-x86_64/ mysql

3、添加mysql用户组和mysql用户
先检查是否有mysql用户组和mysql用户
groups mysql

若无,则添加;
groupadd mysql useradd -r -g mysql mysql

若有,则跳过;

4、进入mysql目录更改权限
cd mysql/ chown -R mysql:mysql ./

5、执行安装脚本
./scripts/mysql_install_db --user=mysql

安装完之后修改当前目录拥有者为root用户,修改data目录拥有者为mysql
chown -R root:root ./ chown -R mysql:mysql data

6、更改mysql密码
上一步安装脚本执行输出的日志中告诉我们如何更改密码了

但是如果直接执行这两条命令就会报错

因为这时还没有启动mysql,这算是一个坑。启动方法如下:
./support-files/mysql.server start

如果MySQL启动报错,则可能是已经存在MySQL进程,杀掉即可
ps aux|grep mysql kill -9 上边的进程号 #或者下边一条命令即可杀掉所有MySQL进程 ps aux|grep mysql|awk '{print $2}'|xargs kill -9

杀掉后再启动即可。

MySQL启动之后再执行如下命令更改密码:
./bin/mysqladmin -u root -h localhost.localdomain password 'root'

密码更改后即可登录MySQL
./bin/mysql -h127.0.0.1 -uroot -proot

登录之后将其他用户的密码也可改为root
update mysql.user set password=password('root') where user='root'; flush privileges;

7、增加远程登录权限
上一步即可本地登录,但远程登录会报错

为解决这一问题,需要本地登陆MySQL后执行如下命令
grant all privileges on *.* to root@'%' identified by 'root'; flush privileges;
