mysql 5.7 初始化数据库_MySQL 5.7 新特性之初始化

本文详细介绍了MySQL 5.7的数据库初始化过程,包括下载解压二进制包,创建软链接,设置环境变量,创建用户和目录结构,生产配置文件,使用mysqld --initialize进行初始化,以及启动数据库。此外,还提到了5.7版本的新特性,如默认生成的root账号密码直接记录在日志中,并讨论了如何设置美观的MySQL命令提示符。
摘要由CSDN通过智能技术生成

1. 把二进制安装包下载放在/opt 目录下并解压

f6fff13d8f98ae9d1b371ede9e4057df.png

2. 创建软连接, 并添加运行环境

ln -s /usr/local/mysql /opt/mysql-5.7.18-linux-glibc2.5-x86_64

[root@M1 local]# ll | grep mysql

lrwxrwxrwx 1 root root 39 Jul 26 12:00 mysql -> /opt/mysql-5.7.18-linux-glibc2.5-x86_64

export PATH=$PATH:$HOME/bin:/usr/local/mysql/bin 加到环境变量

3. 创建mysql 用户

groupadd mysql

useradd -r -g mysql -s /bin/false mysql

4. 创建目录结构和赋权

mkdir /data/mysql/3306/ -p

cd data

mkdir {data,logs,tmp}

chown mysql:mysql -R /data

5.生产cnf 配置文件

推荐使用叶老师的在线cnf 文件生产工具 http://imysql.com/my-cnf-wizard.html

6. 初始化

[root@M1 bin]# ./mysqld --initialize --datadir=/data/mysql/3306/data --user=mysql --basedir=/usr/local/mysql/

2017-07-26T08:24:03.273745Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

2017-07-26T08:24:04.102255Z 0 [Warning] InnoDB: New log files created, LSN=45790

2017-07-26T08:24:04.243150Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.

2017-07-26T08:24:04.314543Z 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: c9102823-71db-11e7-ba5c-005056b643b3.

2017-07-26T08:24:04.321167Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.

2017-07-26T08:24:04.322012Z 1 [Note] A temporary password is generated for root@localhost: 2hpi85k*T-t=

我们可以看到root@localhost 被赋予随机生成的一个密码,这与以往的mysql版本不一样, 另外mysql_install_db 这个工具也不再被推荐了,mysql_install_db is deprecated. Please consider switching to mysqld --initialize

另外,在初始化时如果加上–initial-insecure,则会创建空密码的 root@localhost 账号,否则会创建带密码的 root@localhost 账号,密码直接写在 log-error 日志文件中(在5.6版本中是放在 ~/.mysql_secret 文件里,更加隐蔽,不熟悉的话可能会无所适从)

7.启动数据库

76a8d8f37f2a118b75ae60aa3a82da38.png

8. 使用刚刚生成的密码就可以正常登录了

[root@M1 bin]# mysql -uroot -p -S /data/mysql3306.sock

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 3

Server version: 5.7.18-log

Copyright (c) 2000, 2017, 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>

9. 想要更加美观? 也是可以的

在~/.bash_profile 加上export MYSQL_PS1="\\u@\\h:\\p [\\d]>"

或者可以在[mysql] 添加

[mysql]

prompt=\\u@\\h:\\p [\\d]>

root@localhost:mysql3306.sock [(none)]>show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| sys |

+--------------------+

4 rows in set (0.00 sec)

root@localhost:mysql3306.sock [(none)]>use sys

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

root@localhost:mysql3306.sock [sys]>

==========最后总结======

快速初始化脚本如下:

1. 把压缩文件放在/opt 下面

2. my.cnf 文件的内容需要和以下文件目录匹配

#!/bin/sh

cd /opt

tar zxvf mysql*.tar.gz

ln -s /usr/local/mysql /opt/mysql*linux-glibc2.5-x86_64

cat >> /etc/profile <

export MYSQL_PS1="\\u@\\h:\\p [\\d]>"

export PATH=\$PATH:/usr/local/mysql/bin

EOF

groupadd mysql

useradd -r -g mysql -s /bin/false mysql

mkdir /data/mysql/3306/ -p

cd /data/mysql/3306

mkdir {data,logs,tmp}

chown mysql:mysql -R /data

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值