linux 安装mysql5.6

  1. 下载mysql安装包
powershell 复制代码
https://dev.mysql.com/downloads/mysql/5.6.html
  1. 卸载系统自带的mariadb
powershell 复制代码
[root@gpap-prod-3 ~]# rpm -qa| grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
[root@gpap-prod-3 ~]# rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
warning: /etc/my.cnf saved as /etc/my.cnf.rpmsave

删除/etc下的my.cnf文件

powershell 复制代码
[root@gpap-prod-3 ~]# rm -rf /etc/my.cnf.rpmsave 
[root@gpap-prod-3 ~]# rm -rf /etc/my.cnf
  1. 创建mysql用户组,创建mysql用户,并将其加入mysql用户组
powershell 复制代码
[root@gpap-prod-3 ~]# groupadd mysql
[root@gpap-prod-3 ~]# useradd -r -g mysql mysql
  1. 将下载好的二进制包放到/usr/local目录下
powershell 复制代码
mv mysql-5.6.48-linux-glibc2.12-x86_64.tar.gz /usr/local/

解压文件

powershell 复制代码
tar xf mysql-5.6.48-linux-glibc2.12-x86_64.tar.gz

将其命名为mysql

powershell 复制代码
mv mysql-5.6.48-linux-glibc2.12-x86_64 mysql
  1. 在/etc目录下创建新的my.cnf文件
powershell 复制代码
vim my.cnf

my.cnf文件内容如下:

powershell 复制代码
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
socket=/var/lib/mysql/mysql.sock
[mysqld]
skip-name-resolve
#设置3306端口
port=3306
socket=/var/lib/mysql/mysql.sock
# 设置mysql的安装目录
basedir=/usr/local/mysql
# 设置mysql数据库的数据的存放目录
datadir=/usr/local/mysql/data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M

创建my.cnf中涉及到的目录

powershell 复制代码
mkdir /var/lib/mysql
mkdir /var/lib/mysql/mysql
chown -R mysql:mysql /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql/mysql
pwd
/usr/local/mysql
  1. 进入mysql安装目录(/usr/local/mysql)安装autoconf库和libaio库文件
powershell 复制代码
yum -y install autoconf
yum install libaio* -y
  1. 配置mysql
    修改my.cnf文件权限
powershell 复制代码
chmod 777 /etc/my.cnf

设置开机自动启动服务的脚本

powershell 复制代码
cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
./scripts/mysql_install_db --user=mysql

增加mysqld服务控制脚本执行权限

powershell 复制代码
chmod +x /etc/rc.d/init.d/mysqld

将mysql服务加入到系统服务中

powershell 复制代码
chkconfig --add mysqld

检查服务是否生效

powershell 复制代码
chkconfig --list mysqld

输出日志如下:

powershell 复制代码
Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

mysqld             0:off    1:off    2:on    3:on    4:on    5:on    6:off

启动mysqld服务

powershell 复制代码
systemctl restart mysqld.service

修改 /etc/profile 文件,将mysql的bin目录加入path环境变量

powershell 复制代码
export MYSQL_HOME="/usr/local/mysql"
export PATH="$PATH:MYSQL_HOME/bin"

使profile文件的修改生效

powershell 复制代码
source /etc/profile
  1. 如果设置了防火墙,则需要开通3306和8080端口,依次执行以下命令
powershell 复制代码
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --zone=public --add-port=3306/udp --permanent
firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --zone=public --add-port=8080/udp --permanent
firewall-cmd --reload
  1. 创建mysql软连接
powershell 复制代码
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
  1. 修改mysql用户密码
powershell 复制代码
./mysqld_safe --skip-grant-tables

日志输出如下

powershell 复制代码
Warning: World-writable config file '/etc/my.cnf' is ignored
Warning: World-writable config file '/etc/my.cnf' is ignored
210811 14:53:29 mysqld_safe Logging to '/usr/local/mysql/data/gpap-prod-3.err'.
210811 14:53:29 mysqld_safe A mysqld process already exists

启动mysql客户端(密码为空,直接回车即可)

powershell 复制代码
[root@gpap-prod-3 bin]# mysql -uroot -p
Warning: World-writable config file '/etc/my.cnf' is ignored
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.42 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

输入sql,依次查看数据库列表、进入mysql数据库,查看user表相关信息

powershell 复制代码
show databases;
use mysql;
show tables;
desc user;
select USER,HOST,password from user;

查到的用户列表如下所示

powershell 复制代码
+------+-------------+----------+
| USER | HOST        | password |
+------+-------------+----------+
| root | localhost   |          |
| root | gpap-prod-3 |          |
| root | 127.0.0.1   |          |
| root | ::1         |          |
|      | localhost   |          |
|      | gpap-prod-3 |          |
+------+-------------+----------+
6 rows in set (0.00 sec)

将user为root,host为localhost的这条数据进行修改

powershell 复制代码
update user set password=password("你的root账户的密码"), host="%" where user="root" and host="localhost";

再执行刷新sql

powershell 复制代码
flush privileges;
  1. 退出mysqld_safe,杀掉该进程
powershell 复制代码
ps aux | grep mysqld
kill -9 mysqld_safe对应的进程ID
  1. 重启mysqld服务
powershell 复制代码
systemctl restart mysqld.service
  1. 本地使用navicat进行连接测试成功
相关推荐
&岁月不待人&1 小时前
⏺ Android 录屏缩放异常排查:Pixel 3 XL 上的完美风暴
android
a3158238061 小时前
Android 大图显示策略优化显示(一)
android·算法·图片加载·大图片
tangweiguo030519871 小时前
从零开始:在 Windows 上使用命令行编译 Android .so 动态库(NDK + CMake + Ninja)
android
苏宸啊1 小时前
Linux指令篇(一)
linux·运维·服务器
阿波罗尼亚1 小时前
Tcp SSE Utils
android·java·tcp/ip
我要升天!2 小时前
Linux中《网络基础》
linux·运维·网络
国强_dev2 小时前
在数据库开发和运维中的“错误信息误导(Red Herring)”
运维·adb·数据库开发
鸽芷咕3 小时前
【2025年度总结】时光知味,三载同行:落笔皆是沉淀,前行自有光芒
linux·c++·人工智能·2025年度总结
羑悻的小杀马特3 小时前
指尖敲代码,笔尖写成长:2025年度总结与那些没说出口的碎碎念
linux·c++·博客之星·2025年度总结
晴天¥4 小时前
VMware+Oracle linux LVM/非LVM磁盘扩容
linux·运维·服务器