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进行连接测试成功
相关推荐
deng-c-f6 分钟前
Linux C/C++ 学习日记(22):Reactor模式(二):实现简易的webserver(响应http请求)
linux·c语言·网络编程·reactor·http_server
BTU_YC11 分钟前
CentOS 7 虚拟IP配置指南:使用传统network-scripts实现高可用
linux·tcp/ip·centos
陌路2011 分钟前
LINUX14 进程间的通信 - 管道
linux·网络
大聪明-PLUS21 分钟前
从头开始为 ARM 创建 Ubuntu 映像
linux·嵌入式·arm·smarc
chenzhou__34 分钟前
MYSQL学习笔记(个人)(第十五天)
linux·数据库·笔记·学习·mysql
序属秋秋秋2 小时前
《Linux系统编程之入门基础》【Linux基础 理论+命令】(上)
linux·运维·服务器·ubuntu·centos·命令模式
2501_915918416 小时前
掌握 iOS 26 App 运行状况,多工具协作下的监控策略
android·ios·小程序·https·uni-app·iphone·webview
一张假钞6 小时前
Ubuntu SSH 免密码登陆
linux·ubuntu·ssh
Wang's Blog7 小时前
Linux小课堂: 文件操作警惕高危删除命令与深入文件链接机制
linux·运维·服务器
水月wwww8 小时前
操作系统——进程管理
linux·操作系统·vim·进程·进程调度