zoukankan      html  css  js  c++  java
  • CentOS安装mysql

    1、  mysql安装(此处实例mysql版本为5.6版本)

      a) 下载mysql的repo源

        $ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

      b) 安装mysql-community-release-el7-5.noarch.rpm包

        $ sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm

        安装这个包后,会获得两个mysql的yum repo源:/etc/yum.repos.d/mysql-community.repo,/etc/yum.repos.d/mysql-community-source.repo。

      c) 安装mysql

        $ sudo yum install mysql-community-server

        这将安装MySQL server(mysql-community-server)的包以及运行服务器所需组件的包,包括client(mysql-community-client)的包,客户端和服务器的常见错误消息和字符集(mysql-community-common)以及共享客户端库(mysql-community-libs) 。

        根据提示安装就可以了,不过安装完成后没有密码,需要重置密码

      d)  重置mysql密码

        $ mysql -u root

        登录时有可能报这样的错:ERROR 2002 (HY000): Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘ (2),原因是/var/lib/mysql的访问权限问题。下面的命令把/var/lib/mysql的拥有者改为当前用户:

      e)  $ sudo chown -R root:root /var/lib/mysql

        重启mysql服务

        $ service mysqld restart

      f)  接下来登录重置密码:

              $ mysql -u root  //直接回车进入mysql控制台

              mysql > use mysql;

              mysql >set password=password('123456') where user='root';或者更改 update user set password=password('123456') where user='root';

                 mysql > exit;

      (

        5.7版本以上:服务启动之后,系统默认生成了一个初始密码,可以cat /var/log/mysqld.log 查看初始密码

        $mysql -uroot -p  //然后输入初始密码

         mysql >  ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';   更改初始密码

        注意:实现的默认密码策略要求密码包含至少一个大写字母,一个小写字母,一个数字和一个特殊字符,并且密码总长度至少为8个字符

      )

    修改mysql数据库密码的4种方法

    方法1: 用SET PASSWORD命令
    首先登录MySQL。
    格式:mysql> set password for 用户名@localhost = password('新密码');
    例子:mysql> set password for root@localhost = password('123');

    方法2:用mysqladmin
    格式:mysqladmin -u用户名 -p旧密码 password 新密码
    例子:mysqladmin -uroot -p123456 password 123

    方法3:用UPDATE直接编辑user表
    首先登录MySQL。
    mysql> use mysql;
    mysql> update user set password=password('123') where user='root' and host='localhost';
    mysql> flush privileges;

    方法4:在忘记root密码的时候,可以这样
    以windows为例:
    1. 关闭正在运行的MySQL服务。
    2. 打开DOS窗口,转到mysqlin目录。
    3. 输入mysqld --skip-grant-tables 回车。--skip-grant-tables 的意思是启动MySQL服务的时候跳过权限表认证。
    4. 再开一个DOS窗口(因为刚才那个DOS窗口已经不能动了),转到mysqlin目录。
    5. 输入mysql回车,如果成功,将出现MySQL提示符 >。
    6. 连接权限数据库: use mysql; 。
    6. 改密码:update user set password=password("123") where user="root";(别忘了最后加分号) 。
    7. 刷新权限(必须步骤):flush privileges; 。
    8. 退出 quit。
    9. 注销系统,再进入,使用用户名root和刚才设置的新密码123登录。 

    mysql用户操作

    一、创建用户

    命令:CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 

    备注:username - 你将创建的用户名, host - 指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost, 如果想让该用户可以从任意远程主机登陆,可以使用通配符%. password - 该用户的登陆密码,密码可以为空,如果为空则该用户可以不需要密码登陆服务器.

    二、授权

    命令:GRANT privileges ON databasename.tablename TO 'username'@'host'

    备注: privileges - 用户的操作权限,如SELECT , INSERT , UPDATE 等(详细列表见该文最后面).如果要授予所的权限则使用ALL.;databasename - 数据库名,tablename-表名,如果要授予该用户对所有数据库和表的相应操作权限则可用*表示, 如*. *    username代表你创建的用户名 host代表哪个主机

    例:GRANT all ON databasename.tablename TO 'username'@'host';      //开放给这个用户对于这个数据库所有的权限

      GRANT select ON databasename.tablename TO 'username'@'host';    //只开放给这个用户对这个数据库查询的权限

    创建用户并同时授权

    命令:grant all privileges on *.* to 'test'@'%' identified by 'password';

    三、设置与更改用户密码

    命令:SET PASSWORD FOR 'username'@'host' = PASSWORD('newpassword');如果是当前登陆用户用SET PASSWORD = PASSWORD("newpassword");

    例子: SET PASSWORD FOR 'test'@'%' = PASSWORD("123456"); 

    四.撤销用户权限

    命令: REVOKE privilege ON databasename.tablename FROM 'username'@'host';

    说明: privilege, databasename, tablename - 同授权部分.

    例子: REVOKE SELECT ON *.* FROM 'pig'@'%';

    注意: 假如你在给用户'pig'@'%'授权的时候是这样的(或类似的):GRANT SELECT ON test.user TO 'pig'@'%', 则在使用REVOKE SELECT ON *.* FROM 'pig'@'%';命令并不能撤销该用户对test数据库中user表的SELECT 操作.相反,如果授权使用的是GRANT SELECT ON *.* TO 'pig'@'%';则REVOKE SELECT ON test.user FROM 'pig'@'%';命令也不能撤销该用户对test数据库中user表的Select 权限. 

    五、允许远程访问

    首先使用root身份进入MySQL  

      mysql>use mysql;    //进入mysql表

      mysql> selecthost,user from user;     //查看结果是不是root用户仅允许本地(localhost)登录

    如果不是 执行以下语句修改

    mysql>update mysql.user set Host='%' where HOST='localhost' and User='root';

    mysql>flush privileges;

    执行完后可能提示error.再mysql> select host,user from user;查看下吧.

    如果root用户 host地方是% 的话 就表示可以远程登陆了

    如果遇到了mysql远程可登录,本地无法登陆的问题。或者修改之后 原本root有密码 但是修改之后本地无密码可以登陆了

    原因:mysql中一个用户名为空的访问本地权限的字段,所有本地登陆时优先匹配了这一条,就无法登陆了或者无密码了。

    SELECT USER,HOST FROM USER;查询用户和主机

    执行DROP USER ' '@localhost; 删除那条记录就OK了!

    usermod -g group1 user1

    把user1用户加入group1 组

    六.删除用户

    命令: DROP USER 'username'@'host'; 

    ALTER Allows use of ALTER TABLE.
    ALTER ROUTINE Alters or drops stored routines.
    CREATE Allows use of CREATE TABLE.
    CREATE ROUTINE Creates stored routines.
    CREATE TEMPORARY TABLE Allows use of CREATE TEMPORARY TABLE.
    CREATE USER Allows use of CREATE USER, DROP USER, RENAME USER, and REVOKE ALL PRIVILEGES.
    CREATE VIEW Allows use of CREATE VIEW.
    DELETE Allows use of DELETE.
    DROP Allows use of DROP TABLE.
    EXECUTE Allows the user to run stored routines.
    FILE Allows use of SELECT... INTO OUTFILE and LOAD DATA INFILE.
    INDEX Allows use of CREATE INDEX and DROP INDEX.
    INSERT Allows use of INSERT.
    LOCK TABLES Allows use of LOCK TABLES on tables for which the user also has SELECT privileges.
    PROCESS Allows use of SHOW FULL PROCESSLIST.
    RELOAD Allows use of FLUSH.
    REPLICATION Allows the user to ask where slave or master
    CLIENT servers are.
    REPLICATION SLAVE Needed for replication slaves.
    SELECT Allows use of SELECT.
    SHOW DATABASES Allows use of SHOW DATABASES.
    SHOW VIEW Allows use of SHOW CREATE VIEW.
    SHUTDOWN Allows use of mysqladmin shutdown.
    SUPER Allows use of CHANGE MASTER, KILL, PURGE MASTER LOGS, and SET GLOBAL SQL statements. Allows mysqladmin debug command. Allows one extra connection to be made if maximum connections are reached.
    UPDATE Allows use of UPDATE.
    USAGE Allows connection without any specific privileges.
  • 相关阅读:
    【LeetCode】【动态规划】Edit Distance
    【LeetCode】最大子阵列 Maximum Subarray(贪婪&分治)
    【LeetCode】【矩阵旋转】Rotate Image
    解决Torch.load()错误信息: UnicodeDecodeError: 'ascii' codec can't decode byte 0x8d in position 0: ordinal not in range(128)
    使用VS Code配合Remote Development插件连接远程服务器(Mac/Linux+Windows) | Using VS Code with Remote Development Connect to Remote Server (Mac/Linux+Windows)
    Leaflet入门:添加点线面并导入GeoJSON数据|Tutorial of Leaflet: Adding Points, Lines, Polygons and Import GeoJSON File
    使用Adobe Illustrator + ArcGIS绘制地图 | Map Design Using ArcGIS + Adobe Illustrator
    PostgreSQL 速查、备忘手册 | PostgreSQL Quick Find and Tutorial
    LIRE教程之源码分析 | LIRE Tutorial of Analysis of the Source Code
    解决Tomcat错误信息:No 'Access-Control-Allow-Origin' header is present on the requested resource | Solving Tomcat Error: No 'Access-Control-Allow-Origin' header is present on the requested resource
  • 原文地址:https://www.cnblogs.com/fqszywz/p/7986308.html
Copyright © 2011-2022 走看看