Centos7下MySQL 8.0 基础

  1. yum -y install mysql mysql-server

    • 提示已经安装mariadb 不再安装mysql
    • 提示没有在yum 库中找到mysql-server :
      • 需要首先从mysql官网下载npm包到本地:wget https://mysql.com/get/mysql**-community-release*****.noarch.rpm(具体的mysql版本),参见mysql 官方yum库
      • yum install mysql**-community-release*****.noarch.rpm
      • yum install mysql-server
  2. 确认mysqld服务是否正确运行。尝试登陆时,若异常可能报错Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

    • 查看mysql 3306端口是否打开:lsof -i:3306或者netstat -ntpl |grep 3306

    • 查看mysql服务是否启动service mysqld status 或者 systemctl status mysqld

    • 若上述报错,确认/etc/init.d目录下是否含有msyqld服务(init.d 目录中存放的是一系列系统服务的管理(启动或停止)脚本),若没有

      • 使用find / -name msyqld
      • 将上述找到的目录 复制到/etc/init.d
      • 重启mysql 服务 systemctl restart msyqld , systemctl status mysqld
    • 初次使用root用户以空密码登陆时报错ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) 参考链接

      • 原因:root用户空密码只适用于5.7以下版本

      • 对于5.7以上的版本

        The superuser account 'root'@'localhost' is created. The password for the superuser is set and stored in the error log file

        查看方法grep 'temporary password' /var/log/mysqld.log

  3. 关于用户的创建及密码的修改

    • 首次成功登陆后修改root密码ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass5!'

    • 创建新用户 CREATE USER user_name@host_name|ip IDENTIFIED BY 'pwd_you_set'

    • 查看已有用户的相关信息------相关表mysql.user

      • show [full] columns from mysql.user; desc mysql.user;
      • select user,host,authentication_string from mysql.user; (字段-authentication_string 当前用户pwd使用md5加密后的散列值)
      • select * from mysql.user where user='root'\G
  4. 数据库的授权:

    • 基本语法: GRANT * | ALL PRIVILEGES|SELECT,UPDATE,ALTER,INSERT,DELETE ON db_name.tb_name TO user_name@host_name|ip IDENTIFIED BY 'user_pwd [with grant option] ;'
    • 每次更新权限之后需要刷新权限FLUSH PRIVILEGES;
    • MySQL 8.0 版本之后语法有更新,创建用户语句和权限修改语句需要分开执行
      • 创建账户:create user '用户名'@'访问主机' identified by '密码';
      • 权限修改:grant 权限列表 on 数据库 to '用户名'@'访问主机' ;(修改权限时在后面加with grant option)
    • 示例:新建一个用户 test_admin 并赋予其全部的权限
      • CREATE USER test_admin@host_name IDENTIFIED BY 'pwd_seted';
      • GRANT * ON *.* TO test_admin@host_name WITH GRANT OPTION
      • 修改权限允许test_admin 从任意ip登陆 UPDATE mysql.user SET user.Host='%' where user.User='test_admin';
    • 远程登陆
      • 前提:当前主机在要登陆的用户的许可范围之内 (select host from mysql.user where user='test_admin';------> %表示允许任意主机使用该用户名登陆当前数据库)
      • 登陆方式 mysql -h 目标数据库的IP -u user_name -p
  5. 数据库的导出/导入

    • 导出到本地---备份数据库:mysqldump -u user_ name -p target_db > path/db_name.sql;
    • 恢复数据库--from 本地:
      • ①在主机上操作: mysql -u user_name -p db_name < path/db_name.sql;
      • ② 在mysql内部操作soucre path/db_name ;
  6. mysql 乱码问题解决
    image-20220218103628195

  7. MySQL 8.0 -------root用户密码忘记时的策略:

    • refman 。 https://dev.mysql.com/doc/refman/8.0/en/resetting-permissions.html
      image-20220218161220017

    • --init-file方式失败------衍生问题:

      • linux 上 以 'mysql' 的身份登陆的问题
      • service service_name status systemctl status service_name 差异问题
        image-20220218161902309
    • --skip-grant-tables 方式----成功

      • 停止msyql服务,编辑配置文件/etc/my.cnf

        Stop the MySQL server if necessary, then restart it with the --skip-grant-tables option. This enables anyone to connect without a password and with all privileges,

      • 重启mysql并登陆 systemtl restart mysqld ; mysql 直接回车进行登陆

      • 刷新权限并更改root用户的pwd: flush privileges ; alter user 'root'@'localhost' identified by 'NewPassWd'

      • 停止mysql服务,并将之前配置文件(/etc/my.cnf)中添加的skip-grant-tables注释掉,之后重启mysql服务,以新设置的pwd进行正常登陆:systemctl stop mysqld ;vi /etc/my.cnf; #skip-grant-tables; systemctl restart mysqld ; mysql -uroot -p

posted @ 2022-02-12 15:56  Walker-r  阅读(29)  评论(0编辑  收藏  举报