Linux下安装MySQL
原文链接:https://www.jianshu.com/p/f4a98a905011
环境:
1、操作系统:CentOS
2、安装版本: mysql-5.6.31-linux-glibc2.5-x86_64.tar.gz
3、特殊说明:Linux下MySQL5.6与MySQL5.7安装方法略有不同,本文以5.6为主
安装步骤
卸载老版本MySQL
1.查找并删除mysql有关的文件
find / -name mysql
rm -rf 上边查找到的路径,多个路径用空格隔开
#或者下边一条命令即可
find / -name mysql|xargs rm -rf
![](http://upload-images.jianshu.io/upload_images/1899977-305617e34955e4d3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
tar -zxvf mysql-5.6.31-linux-glibc2.5-x86_64.tar.gz
![](http://upload-images.jianshu.io/upload_images/1899977-f8b6174388c8531d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
rm -f mysql-5.6.31-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.6.31-linux-glibc2.5-x86_64/ mysql
![](http://upload-images.jianshu.io/upload_images/1899977-7c5b2ca42cf623e1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
4、添加mysql用户组和mysql用户
先检查是否有mysql用户组和mysql用户
groups mysql
![](http://upload-images.jianshu.io/upload_images/1899977-cc398fb960e0cb76.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/443/format/webp)
若无,则添加;
groupadd mysql
useradd -r -g mysql mysql
![](http://upload-images.jianshu.io/upload_images/1899977-f80edc428a9e61cf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/591/format/webp)
若有,则跳过;
![](http://upload-images.jianshu.io/upload_images/1899977-6aee55a3221c0446.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/426/format/webp)
5、进入mysql目录更改权限
cd mysql/
chown -R mysql:mysql ./
![](http://upload-images.jianshu.io/upload_images/1899977-ad018015ea413b53.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/771/format/webp)
6、执行安装脚本
./scripts/mysql_install_db --user=mysql
出现如下异常:
FATAL ERROR: please install the following Perl modules before executing /usr/bin/mysql_install_db:
Data::Dumper
解决方案:
]# yum install -y perl-Module-Install.noarch
原因:
在centos7环境中没有找到离线安装的办法,只能在线安装,需要调整到可联网状态。虚拟机中的话,可以调整到nat模式下。
先执行yum install -y perl-Module-Install.noarch,然后执行安装脚本 ./scripts/mysql_install_db --user=mysql
![](http://upload-images.jianshu.io/upload_images/1899977-e07b0b2383d9fc81.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
安装完之后修改当前目录拥有者为root用户,修改data目录拥有者为mysql
chown -R root:root ./
chown -R mysql:mysql data
![](http://upload-images.jianshu.io/upload_images/1899977-4e2b1c5c2fd75d24.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/877/format/webp)
7、更改mysql密码
上一步安装脚本执行输出的日志中告诉我们如何更改密码了
![](http://upload-images.jianshu.io/upload_images/1899977-ccd379696682e6c6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/855/format/webp)
但是如果直接执行这两条命令就会报错
![](http://upload-images.jianshu.io/upload_images/1899977-151811b1ccfd6b75.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/950/format/webp)
因为这时还没有启动mysql,这算是一个坑。启动方法如下:
./support-files/mysql.server start
![](http://upload-images.jianshu.io/upload_images/1899977-31eacd5fca58b5b5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/765/format/webp)
如果MySQL启动报错,则可能是已经存在MySQL进程,杀掉即可
ps aux|grep mysql
kill -9 上边的进程号
#或者下边一条命令即可杀掉所有MySQL进程
ps aux|grep mysql|awk '{print $2}'|xargs kill -9
![](http://upload-images.jianshu.io/upload_images/1899977-6572db71fdf5faa7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
杀掉后再启动即可。
![](http://upload-images.jianshu.io/upload_images/1899977-f417bdc69058de74.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/794/format/webp)
MySQL启动之后再执行如下命令更改密码:
./bin/mysqladmin -u root -h localhost.localdomain password 'root'
![](http://upload-images.jianshu.io/upload_images/1899977-d1a1c4478febf199.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
密码更改后即可登录MySQL
./bin/mysql -h127.0.0.1 -uroot -proot
![](http://upload-images.jianshu.io/upload_images/1899977-c22b674ff3f96009.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/939/format/webp)
登录之后将其他用户的密码也可改为root
update mysql.user set password=password('root') where user='root';
flush privileges;
![](http://upload-images.jianshu.io/upload_images/1899977-010a473ab72f59fc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/992/format/webp)
mysql命令 出现-bash: mysql: command not found的方法
原因:因为系统默认会查找/usr/bin下的命令,由于mysql没有在这个目录下,所以出现not found。因此需要做一个软连接到/usr/bin目录下。
执行命令: ln -s /usr/local/mysql/bin/mysql /usr/bin 即可。
8、增加远程登录权限
上一步即可本地登录,但远程登录会报错
![](http://upload-images.jianshu.io/upload_images/1899977-2185d688960a5413.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/502/format/webp)
为解决这一问题,需要本地登陆MySQL后执行如下命令
grant all privileges on *.* to root@'%' identified by 'root';
flush privileges;
![](http://upload-images.jianshu.io/upload_images/1899977-8488efcf4d8d96a1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/878/format/webp)
执行之后即可远程登录
![](http://upload-images.jianshu.io/upload_images/1899977-45f2148fec4a53af.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/486/format/webp)
9、将MySQL加入Service系统服务
cp support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
service mysqld restart
service mysqld status
![](http://upload-images.jianshu.io/upload_images/1899977-e8c7c78c63097796.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/900/format/webp)
![](http://upload-images.jianshu.io/upload_images/1899977-ba65cfbc31d07567.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/833/format/webp)
10、配置my.cnf
vim my.cnf
#添加以下两条语句并保存退出
character-set-server=utf8
lower_case_table_names=1
max_allowed_packet=100M
![](http://upload-images.jianshu.io/upload_images/1899977-d145484bd42a4b63.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/840/format/webp)
配置好之后,重启mysqld服务
![](http://upload-images.jianshu.io/upload_images/1899977-c7178785535c5175.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/872/format/webp)