MySQL 5.7 新特性之初始化

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

MySQL 5.7 新特性之初始化

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.启动数据库

MySQL 5.7 新特性之初始化

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 <<EOF
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

上一篇:一个.NET通用JSON解析/构建类的实现(c#)转


下一篇:centos 7 禁用笔记本触摸板设置