mysql5.7 初始化

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/rznice/article/details/79610893

oracle网站下载mysql(5.7)的压缩文件并解压。
要初始化数据目录,调用 mysqld的与 - -initialize或 - -initialize不安全的选项,这取决于你是否希望服务器生成的一个随机初始密码’root’@’本地账号’的账户,data目录下生成一个以err结尾的文件,里面有root@localhost的临时的密码:。
windows下使用mysqld - -initialize或者mysqld - -initialize-insecure
windows下使用mysqld - -initialize,会生成一个临时的密码,可以通过 服务器管理器 诊断 事件查看器 windows日志 应用程序 查看mysql的日志信息:

这里写图片描述

在Unix和类Unix系统,重要的是要确保数据库的目录和文件都是由拥有 MySQL的登录帐户,以便服务器具有读写访问它们,当你以后运行它。为了确保这一点,如果你运行的mysqld为root,包括 –user选项,如下所示:

 shell> bin/mysqld --initialize --user=mysql
 shell> bin/mysqld --initialize-insecure --user=mysql
2018-03-19T02:14:39.680642Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-03-19T02:14:40.367043Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-03-19T02:14:40.491843Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-03-19T02:14:40.507444Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 47e670bc-2b1b-11e8-bd1b-005056bbabb6.
2018-03-19T02:14:40.523044Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-03-19T02:14:44.766251Z 0 [Warning] CA certificate ca.pem is self signed.
2018-03-19T02:14:45.593052Z 1 [Note] A temporary password is generated for root@localhost: IYers=hp(0hC   // 此处是root用户的临时密码信息。
2018-03-19T02:15:49.802765Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-03-19T02:15:49.802765Z 0 [Note] --secure-file-priv is set to NULL. Operations related to importing and exporting data are disabled
2018-03-19T02:15:49.802765Z 0 [Note] MySQL (mysqld 5.7.21-enterprise-commercial-advanced) starting as process 213416 ...
2018-03-19T02:15:49.802765Z 0 [Note] InnoDB: Mutexes and rw_locks use Windows interlocked functions
2018-03-19T02:15:49.802765Z 0 [Note] InnoDB: Uses event mutexes
2018-03-19T02:15:49.802765Z 0 [Note] InnoDB: _mm_lfence() and _mm_sfence() are used for memory barrier
2018-03-19T02:15:49.818365Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3
2018-03-19T02:15:49.818365Z 0 [Note] InnoDB: Number of pools: 1
2018-03-19T02:15:49.818365Z 0 [Note] InnoDB: Not using CPU crc32 instructions
2018-03-19T02:15:49.818365Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2018-03-19T02:15:49.833965Z 0 [Note] InnoDB: Completed initialization of buffer pool
2018-03-19T02:15:49.849565Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
2018-03-19T02:15:49.943165Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2018-03-19T02:15:49.943165Z 0 [Note] InnoDB: Setting file '.\ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2018-03-19T02:15:49.989966Z 0 [Note] InnoDB: File '.\ibtmp1' size is now 12 MB.
2018-03-19T02:15:49.989966Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
2018-03-19T02:15:49.989966Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
2018-03-19T02:15:49.989966Z 0 [Note] InnoDB: Waiting for purge to start
2018-03-19T02:15:50.052366Z 0 [Note] InnoDB: 5.7.21 started; log sequence number 2551175

初始化后,运行:mysqld MySQL启动mysql服务。
登录

mysql -hlocalhost -u root -p
Enter password:******

登录后:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.21-enterprise-commercial-advanced

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>                                                                                                                                                                    

查看数据库:

mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql>

提示修改mysql密码:

alter user 'root'@'localhost' identified by 'abc23';
或者:
set password for root@localhost = password('abc23');
也可以:
SET PASSWORD FOR 'root'@'host_name' = PASSWORD('newpwd');
SET PASSWORD FOR 'root'@'%' = PASSWORD('newpwd');

用服务器主机名替换第二个SET PASSWORD语句中的host_name。这是你指定匿名账户密码的主机名。
要想使用mysqladmin为root账户指定密码,执行下面的命令:
  shell> mysqladmin -u root password "newpwd"
  shell> mysqladmin -u root -h host_name password "newpwd"
  上述命令适用于Windows和Unix。用服务器主机名替换第二个命令中的host_name。
  不一定需要将密码用双引号引起来,但是你如果密码中包含空格或专用于命令解释的其它字符,

密码修改方式:
方法1、
update user set password=PASSWORD("123456")where user="root"; #更改密码为 newpassord
mysql>flush privileges; #更新权限
方法2、
shell>service mysqld stop #停止mysql服务
shell>mysqld_safe --skip-grant-tables & #以不启用grant-tables模式启动mysql
shell>mysql -uroot -p #输入命令回车进入,出现输入密码提示直接回车。
mysql > set password for root@localhost = password('mysqlroot');

方法3、
shell>/path/mysqladmin -u UserName -h Host password 'new_password' -p

创建数据库:
CREATE DATABASE DBNAME DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

更改数据库的字符编码
ALTER DATABASE DBNAME DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci

创建用户:

CREATE USER 'username'@'host' IDENTIFIED BY 'password';

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

CREATE USER 'test'@'localhost' IDENTIFIED BY '123456';
CREATE USER 'test'@'192.168.11.11' IDENDIFIED BY '123456';
CREATE USER 'test'@'%' IDENTIFIED BY '123456';
CREATE USER 'test'@'%' IDENTIFIED BY '';
CREATE USER 'test'@'%';

授权远程访问:

GRANT privileges ON databasename.tablename TO 'username'@'host'

说明:
privileges:用户的操作权限,如SELECT,INSERT,UPDATE等,如果要授予所的权限则使用ALL
databasename:数据库名
tablename:表名,如果要授予该用户对所有数据库和表的相应操作权限则可用表示,如.*

GRANT SELECT, INSERT ON test.user TO 'test'@'%';
GRANT ALL ON *.* TO 'test'@'%';
GRANT ALL ON maindataplus.* TO 'test'@'%';

用以上命令授权的用户不能给其它用户授权,如果想让该用户可以授权,用以下命令:

mysql> grant all privileges on *.* to 'root'@'%' identified by '********' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges ;
Query OK, 0 rows affected (0.00 sec)

撤销用户授权:

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

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

REVOKE SELECT ON *.* FROM 'test'@'%';

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

具体信息可以用命令SHOW GRANTS FOR ‘test’@’%’; 查看。

删除用户:

DROP USER 'username'@'host';

参考:
http://www.xiazai.com/article31378

猜你喜欢

转载自blog.csdn.net/rznice/article/details/79610893