CentOS release 6.5---7.4安装MySQL方法及常用命令梳理

本文提供在Linux CentOS 7.2环境下,安装MySQL 5.6.31的详细步骤,包括卸载旧版本、解压安装包、权限设置、密码修改、远程登录配置、服务注册及my.cnf配置等关键操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Linux 系统下安装MySQL

环境:
1、操作系统:CentOS release 7.2 (Final)  --6.5以上-7.4以下都可以采用以下方法安装
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

https://upload-images.jianshu.io/upload_images/1899977-305617e34955e4d3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700

Paste_Image.png

1、在安装包存放目录下执行命令解压文件:

tar -zxvf mysql-5.6.31-linux-glibc2.5-x86_64.tar.gz

https://upload-images.jianshu.io/upload_images/1899977-f8b6174388c8531d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700

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

https://upload-images.jianshu.io/upload_images/1899977-7c5b2ca42cf623e1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700

3、添加mysql用户组和mysql用户

先检查是否有mysql用户组和mysql用户

groups mysql

https://upload-images.jianshu.io/upload_images/1899977-cc398fb960e0cb76.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/443

若无,则添加;

groupadd mysql

useradd -r -g mysql mysql

https://upload-images.jianshu.io/upload_images/1899977-f80edc428a9e61cf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/591
 

若有,则跳过;

https://upload-images.jianshu.io/upload_images/1899977-6aee55a3221c0446.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/426

4、进入mysql目录更改权限

cd mysql/

chown -R mysql:mysql ./

https://upload-images.jianshu.io/upload_images/1899977-ad018015ea413b53.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700

5、执行安装脚本

./scripts/mysql_install_db --user=mysql

https://upload-images.jianshu.io/upload_images/1899977-e07b0b2383d9fc81.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700

安装完之后修改当前目录拥有者为root用户,修改data目录拥有者为mysql

chown -R root:root ./

chown -R mysql:mysql data

https://upload-images.jianshu.io/upload_images/1899977-4e2b1c5c2fd75d24.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700

6、更改mysql密码

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

修改root用户的密码,初始密码为空,执行命令:

首先进入mysql安装目录下:

./bin/mysqladmin -u root password '1qaz!QAZ'

注意:

执行此命令会提示:Warning: Using a password on the command line interface can be insecure,可忽略此警告

进入mysql,执行命令:

mysql -u root -p,然后enter password

 

创建用户:

CREATE USER 'zhaoduwang'@'%' IDENTIFIED BY '1qaz!QAZ';

%:该用户可以从任意远程主机登陆

用iflytek登陆

mysql -u zhaoduwang -h 172.31.3.212 -p

注意:

若输入密码报错:ERROR 1045 (28000): Access denied for user 'zhaoduwang'@'suse-06' (using password: YES),

需要在root用户下执行sql语句

CREATE USER 'zhaoduwang '@'slave1 ' IDENTIFIED BY '1qaz!QAZ ';    ---slave1主机名

创建数据库saa,执行sql语句(随安装包一起发出的数据库初始化sql语句):

create database saa;

授权 :

grant all on saa.* to 'zhaoduwang'@'%';

 

https://upload-images.jianshu.io/upload_images/1899977-ccd379696682e6c6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700

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

https://upload-images.jianshu.io/upload_images/1899977-151811b1ccfd6b75.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700

因为这时还没有启动mysql,这算是一个坑。启动方法如下:

./support-files/mysql.server start

https://upload-images.jianshu.io/upload_images/1899977-31eacd5fca58b5b5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700

如果MySQL启动报错,则可能是已经存在MySQL进程,杀掉即可

ps aux|grep mysql

kill -9 上边的进程号

#或者下边一条命令即可杀掉所有MySQL进程

ps aux|grep mysql|awk '{print $2}'|xargs kill -9

https://upload-images.jianshu.io/upload_images/1899977-6572db71fdf5faa7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700

杀掉后再启动即可。

https://upload-images.jianshu.io/upload_images/1899977-f417bdc69058de74.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700

MySQL启动之后再执行如下命令更改密码:

./bin/mysqladmin -u root -h localhost.localdomain password 'root'        ---localhost.localdomain主机名根据实际情况修改

https://upload-images.jianshu.io/upload_images/1899977-d1a1c4478febf199.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700

密码更改后即可登录MySQL

./bin/mysql -h 127.0.0.1 -u root -p root

https://upload-images.jianshu.io/upload_images/1899977-c22b674ff3f96009.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700

登录之后将其他用户的密码也可改为root

update mysql.user set password=password('root') where user='root';

flush privileges;

https://upload-images.jianshu.io/upload_images/1899977-010a473ab72f59fc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700

7、增加远程登录权限

上一步即可本地登录,但远程登录会报错

https://upload-images.jianshu.io/upload_images/1899977-2185d688960a5413.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/502

为解决这一问题,需要本地登陆MySQL后执行如下命令

grant all privileges on *.* to root@'%' identified by 'root';

flush privileges;

以上语句如果不行可执行以下语句:

1、GRANT ALL PRIVILEGES ON *.* TO 'root'@'slave1' IDENTIFIED BY '1qaz!QAZ' WITH GRANT OPTION;    --slave1是主机名

2、flush privileges;

https://upload-images.jianshu.io/upload_images/1899977-8488efcf4d8d96a1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700

 

执行之后即可远程登录

https://upload-images.jianshu.io/upload_images/1899977-45f2148fec4a53af.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/486

8、将MySQL加入Service系统服务

先进入mysql安装目录下

cp support-files/mysql.server /etc/init.d/mysqld

chkconfig --add mysqld

chkconfig mysqld on

service mysqld restart

 service mysqld status 

https://upload-images.jianshu.io/upload_images/1899977-e8c7c78c63097796.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700

https://upload-images.jianshu.io/upload_images/1899977-ba65cfbc31d07567.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700

9、配置my.cnf

通常my.cnf文件在etc/目录下

 

vim my.cnf

#添加以下两条语句并保存退出

character-set-server=utf8

lower_case_table_names=1

max_allowed_packet=100M

https://upload-images.jianshu.io/upload_images/1899977-d145484bd42a4b63.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700

配置好之后,重启mysqld服务

 

https://upload-images.jianshu.io/upload_images/1899977-c7178785535c5175.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700

注意说明:

1、yum安装的mysql启动命令

 

2、如发现mysql没有启动命令时可采用以下方法试试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值