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 小时前
centos 7.9安装tomcat,并实现开机自启
linux·运维·centos
fangeqin1 小时前
ubuntu源码安装python3.13遇到Could not build the ssl module!解决方法
linux·python·ubuntu·openssl
爱奥尼欧3 小时前
【Linux 系统】基础IO——Linux中对文件的理解
linux·服务器·microsoft
超喜欢下雨天3 小时前
服务器安装 ros2时遇到底层库依赖冲突的问题
linux·运维·服务器·ros2
tan77º4 小时前
【Linux网络编程】网络基础
linux·服务器·网络
还鮟4 小时前
CTF Web的数组巧用
android
笑衬人心。4 小时前
Ubuntu 22.04 + MySQL 8 无密码登录问题与 root 密码重置指南
linux·mysql·ubuntu
小蜜蜂嗡嗡5 小时前
Android Studio flutter项目运行、打包时间太长
android·flutter·android studio
aqi005 小时前
FFmpeg开发笔记(七十一)使用国产的QPlayer2实现双播放器观看视频
android·ffmpeg·音视频·流媒体
chanalbert6 小时前
CentOS系统新手指导手册
linux·运维·centos