MySQL 5.7及mysql 8.0安装和初始化

(默认MySQL 5.7/8.0通用,特殊部分会在文中说明)

一、WINDOWS下安装

详细步骤,参见这个文档:

文档中,涉及以下几个小结:


——解压,然后配置环境变量(也可以不配置,配置只是为了方便执行命令)


——创建my.ini配置文件,可以放到安装目录下,也可以放到C:\WINDOWS或者C:\,内容如下:
[mysqld]
# set basedir to your installation path
basedir=D:/C/dev-solft/mysql-5.7.18-winx64
# set datadir to the location of your data directory
datadir=D:/C/dev-solft/mydata/data
# add by zollty

explicit_defaults_for_timestamp=true


——以管理员身份运行cmd.exe,执行如下命令:
mysqld --initialize-insecure

至此,安装结束,下面是启动。


——以管理员身份运行cmd.exe,执行如下命令:
mysqld --console
输入一堆东西,只要没错就行了,查看进程,多了一个mysqld.exe的进程
(加上--console是为了在console中显示log)

如果不加--console,则为后台启动,可以关掉cmd窗口


运行如下命令:
mysql -u root --skip-password
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
注意:5.7.6之前版本修改密码的方式不一样:
As of MySQL 5.7.6, use ALTER USER:
mysql> ALTER USER user IDENTIFIED BY 'new_password';
Before 5.7.6, use SET PASSWORD:
mysql> SET PASSWORD FOR user = PASSWORD('new_password');
关闭mysqld.exe进程:

mysqladmin -u root -p shutdown


——命令如下:
mysqld --install(随windows启动)
mysqld --install-manual(手动启动,作用不大,手动启动直接mysqld即可,没必要安装服务)

mysqld --remove(移除服务)


2.3.5.9 Testing The MySQL Installation


二、Debian/Ubuntu Linux下安装
参见:


方式一:apt-get

参见:

https://dev.mysql.com/downloads/repo/apt/(下载地址:https://repo.mysql.com//mysql-apt-config_0.8.9-1_all.deb

https://dev.mysql.com/doc/mysql-apt-repo-quick-guide/en/


方式二: 下载 对应系统的tar包:mysql-server_MVER-DVER_CPU.deb-bundle.tar

下载地址:https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-community-server_5.7.20-1ubuntu16.04_amd64.deb

执行以下命令即可:
tar -xvf mysql-server_MVER-DVER_CPU.deb-bundle.tar
sudo apt-get install libaio1
sudo apt-get install libnuma1
sudo dpkg-preconfigure mysql-community-server_*.deb
sudo dpkg -i mysql-{common,community-client,client,community-server,server}_*.deb
如果有报错,执行:
sudo apt-get -f install
然后再次执行上一次的安装命令即可。
解疑:mysql-client和mysql-server是什么关系,可以不安装mysql-client吗?
回答:mysql-client用于使用命令行方式连接mysql服务器,其实是一个CLI Client,建议安装。
The mysql-client package allows you to connect to a MySQL server. It will give you the "mysql" command-line program.
The mysql-server package allows you to run a MySQL server which can host multiple databases and process queries on those databases. It will give you the MySQL daemon, a.k.a. mysqld.
The "MySQL" package probably includes both of the above.
If you just need to connect to a remote server and run queries, install just mysql-client. If you need to host a database, install the client and server.


三、rpm包安装


下载 mysql***.tar 包,例如:MySQL-5.6.42-1.el6.x86_64.rpm-bundle.tar

然后 tar xf 解压,只需依次 使用rpm -ivh命令 安装 server、devel、client、shared即可。

安装好之后,service mysql start 启动服务。注意,安装server时,会初始化root密码,安装信息里面会有如下提示:

A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER !

You will find that password in '/root/.mysql_secret'.


四、tar包安装

1、官网下载mysql-5.7.38-linux-glibc2.12-x86_64.tar安装包

2、解压到一个目录,例如 /opt/mysql-5.7

3、创建配置文件,常用配置如下:

cat /opt/mysql-5.7/my.cnf

join_buffer_size = 128M
sort_buffer_size = 2M
read_rnd_buffer_size = 2M

max_connections=2048

user = mysql
port = 2376

datadir=/data/mysql57
socket=/data/mysql57/mysql.sock

log-error=/opt/mysql-5.7/mysqld.log
pid-file=/data/mysql57/mysqld.pid
lc-messages-dir=/opt/mysql-5.7/share

# 不区分大小写,mysql8.0版本只能在安装初始化时执行
lower-case-table-names=1
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'

4、执行初始化:

bin/mysqld --defaults-file=/opt/mysql-5.7/my.cnf --initialize  --lower-case-table-names=1

注意,mysql 8.0要设置大小写不敏感,必须在初始化数据库时指定: --lower-case-table-names=1,后续再执行就不被允许了。

5、从初始化执行的mysqld.log中找到root账号的密码

6、登录

mysql -uroot -p -S /data/mysql57/mysql.sock

mysql -h127.0.0.1 -uroot --port 2376 -p

第一次进去必须修改root密码:

mysql > SET PASSWORD = PASSWORD('123456');


五、初始配置

1、创建数据库
CREATE DATABASE mydb
DEFAULT CHARACTER SET utf8
DEFAULT COLLATE utf8_general_ci;
2、创建用户,并授权
参见:
CREATE USER 'zollty'@'localhost' IDENTIFIED BY '123456';
GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER,INDEX,REFERENCES ON mydb.* TO 'zollty'@'localhost';

注意,localhost只能在本地使用,如果想不限制hostname,可以设置为 %,例如:

SET PASSWORD FOR 'root'@'%' = PASSWORD('new_password');



四、MySQL 5.6版本 在Linux下安装后root登录

安装过程省略。在安装结束后会有这么一段提示:

A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER !

You will find that password in '/root/.mysql_secret'.

打开这个文件查看root的密码。

然后登录:

$ mysql -u root -p

输入密码即可。

然后修改root的密码:

mysql > SET PASSWORD = PASSWORD('123456');

详见官方文档:https://dev.mysql.com/doc/refman/5.6/en/default-privileges.html#default-privileges-assigning-root-account-passwords





© 2009-2020 Zollty.com 版权所有。渝ICP备20008982号