Linux 下安装 MySQL

10 篇文章 0 订阅
4 篇文章 0 订阅
卸载自带或原有的 MySQL
  1. 检查是否安装了 MySQL:rpm -qa | grep mysql在这里插入图片描述
    2.关闭 MySQL 服务并卸载安装自带的mysql版本:
    关闭 MySQL 服务:service mysql stop
    在这里插入图片描述
    说明服务未启动
    卸载 MySQL:rpm -e mysql-libs-5.1.71.1.el6.x86_64
    在这里插入图片描述
    报错误说明有依赖包
    在这里插入图片描述
    删除成功

    rpm -e 文件名 --nodeps
    如果提示错误:error:%preun(xxxxxx) scriptlet failed,exit status
    则用以下命令尝试
    rpm -e --noscripts
    
    
  2. 查找之前老版本mysql,并且删除老版本mysql的文件和库
    在这里插入图片描述
    如果有文件就删除
    最后检测看是否还有安装的mysql操作:rpm -qa | grep mysql


安装 MySQL
  1. 下载 MySQL
    到mysql官网下载mysql编译好的二进制安装包,在下载页面Select Platform:选项选择linux-generic,然后把页面拉到底部,64 位系统下载Linux - Generic (glibc 2.5) (x86, 64-bit),32位系统下载Linux - Generic (glibc 2.5) (x86, 32-bit)

  2. 解压下载好的 MySQL
    进入安装包所在目录,执行命令:tar -zxvf mysql-5.7.25-linux-glibc2.5-i686.tar.gz -C /usr/local/

  3. 文件重命名
    进入解压后 MySQL 所在的目录:cd /usr/local/
    重命名执行命令:mv mysql-5.7.25-linux-glibc2.5-×86_64/ mysql

  4. 检查 mysql 组和用户是否存在,若无则创建
    检测 mysql 组是否存在:cat /etc/group | grep mysql
    检测 mysql 用户是否存在:cat /etc/passwd | grep mysql
    创建 mysql 组:groupadd mysql
    创建 mysql 用户:useradd -r -g mysql mysql

  5. 在 mysql 下添加 data 目录
    添加 data 目录:mkdir /usr/local/mysql/data

  6. 更改 mysql 下所有目录及文件所属组,用户和权限
    修改所属组和用户:chown -R mysql:mysql /usr/local/mysql/
    修改权限:chmod -R 755 /usr/local/mysql/

  7. 编译安装并初始化 mysql,并记住命令行末尾的密码
    初始化:/usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql
    在这里插入图片描述
    比如我的 MySQL 的密码是:cJhKEJ7aan-c
    这是初始化 MySQL 时自动生成的一个随机密码,要记住

  8. 做个软连接并重启服务
    软连接:ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
    重启服务:service mysql restart

  9. 登录 MySQL 输入密码(密码为步骤 7 初始化生成的密码)
    登录数据库:mysql -u root -p

  10. 修改密码并开放远程
    修改密码:alter user 'root'@'localhost' identified by 'root';
    进入 mysql 库:use mysql;
    开放远程:update user set user.Host='%' where user.User='root';
    刷新:flush privileges;
    退出数据库:quit

以下可以不操作

  1. 编辑 my.cnf,添加配置文件,配置内容为
    vim /etc/my.cnf
    在my.cnf文件中添加或者修改相关配置,更改完成后保存退出

    # For advice on how to change settings please see
    # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
    # *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
    # *** default location during install, and will be replaced if you
    # *** upgrade to a newer version of MySQL.
    
    [mysqld]
    
    # Remove leading # and set to the amount of RAM for the most important data
    # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
    # innodb_buffer_pool_size = 128M
    
    # Remove leading # to turn on a very important data integrity option: logging
    # changes to the binary log between backups.
    # log_bin
    
    # These are commonly set, remove the # and set as required.
    basedir = /usr/local/mysql
    datadir = /usr/local/mysql/data
    port = 3306
    # server_id = .....
    socket = /tmp/mysql.sock
    character-set-server = utf8
    skip-name-resolve
    log-err = /usr/local/mysql/data/error.log
    pid-file = /usr/local/mysql/data/mysql.pid
    
    # Remove leading # to set options mainly useful for reporting servers.
    # The server defaults are faster for transactions and fast SELECTs.
    # Adjust sizes as needed, experiment to find the optimal values.
    # join_buffer_size = 128M
    # sort_buffer_size = 2M
    # read_rnd_buffer_size = 2M 
    
    sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
    
  2. 设置开机启动

    1. 将服务文件拷贝到 init.d 下,并重命名为 mysql:cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
    2. 赋予可执行权限:chmod +x /etc/init.d/mysqld
    3. 添加服务:chkconfig --add mysqld
    4. 显示服务列表:chkconfig --list
    5. 重启服务:service mysqld restart

常用命令
命令描述备注
show databases查看所有数据库
use default进入指定数据库
show tables显示当前数据库下所有数据表
create database business创建一个名叫“business”的数据库
create table t_user(int id, name string)创建一个名叫“t_user”的数据表Hive表中的分隔符只支持一个字符,字段分割符默认是/u0001,即一个半角空格
create table t_user(int id, name string) row format delimited fields terminated by ‘,’创建一个名叫“t_user”的数据表,字段内容以逗号分隔
load data [local] inpath ‘/input/userInfo.txt’ overwrite into table t_user将linux上的文件写入数据表
select count(id) from t_user查询数据表的记录总数调用mapreduce进行统计
desc t_user显示指定数据表的结构
describe t_user显示指定数据表的结构
create table t_user_2 like t_user新建一张数据表t_user_2,表结构完全参照t_user
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值