MySQL的安装

背景

每次重搭环境都免不了要重新安装数据库,虽然频率不高,也发现竟然在3个平台上都装过了,记录一下。

Windows安装MySQL

  • 下载MySql:http://dev.mysql.com/downloads/mysql/
  • 解压后放到安装目录
  • 在环境变量中将mysql安装目录定义为%MYSQL_HOME%,并将bin目录加入环境变量(%MYSQL_HOME%\bin
  • 在mysql-5.6.24-winx64的根目录下,找到my-default.ini文件,改名为my.ini 。打开,添加如下信息:
1
2
3
4
5
6
7
8
9
[mysqld]
loose-default-character-set = utf8
character-set-server = utf8
basedir = D:\mysql-5.6.24-winx64 #写自己的mysql路径哦
datadir = D:\mysql-5.6.24-winx64\data #写自己的mysql路径哦!
[client]
loose-default-character-set = utf8
[WinMySQLadmin]
Server = D:\mysql-5.6.24-winx64\bin\mysqld.exe # 写自己的mysql路径哟!
  • 以管理员身份运行cmd,进入到%MYSQL_HOME%到bin目录下运行如下命令:
1
2
mysqld --initialize -insecure # 初始化mysql,创建root用户,密码为空
mysqld -install

最后提示:Service successfully in installed!

  • 启动mysql。
1
net start mysql
  • 停止mysql
1
net stop mysql
  • 首次登陆,无需密码
1
mysql -u root
  • 修改root密码
1
mysql> set password for root@localhost = password('root');
  • 再次登陆
1
mysql -u root -p

会提示输入密码,输入后已root用户进入。

其他命令

  • 停止mysql
1
net stop mysql
  • 卸载服务
1
mysqld -remove

MAC下安装Mysql

  • 下载Mac版的DMG Archive包。(不建议用brew安装,后续配置很麻烦)
  • 后双击安装,一路下一步
  • 会出现一个提醒,给了个默认root@localhost账号的密码。比如:
    root@localhost: wuKgf_mCK38z

  • 安装完成后,在系统偏好设置中找到MySQL图标,点击进入,手动启动MySQL服务。

  • 通过alias绑定命令:在命令行中运行如下命令,绑定mysql

1
2
MacBook-Air:~ icbc$ alias mysql=/usr/local/mysql/bin/mysql
MacBook-Air:~ icbc$ alias mysqladmin=/usr/local/mysql/bin/mysqladmin

​ 注意:这种方式只能在当前命令行中有效。

  • 建议是在环境变量中增加:
1
2
3
4
5
6
cd ~
touch .bash_profile
vi .bash_profile

# 加入如下语句
export PATH=${PATH}:/usr/local/mysql/bin
  • 进行root密码重置,比如重置为root,运行如下命令,然后输入临时密码即可。
1
2
MacBook-Air:~ icbc$ mysqladmin -u root -p password root
Enter password:

Suse下离线安装MySQL

  • 先下载Suse版本的RPM包,官网有很多,要下载Bundle版,否则得分别下载各个组件。

    SUSE Linux Enterprise Server 12 (x86, 64-bit), RPM Bundle

  • 解压安装包

    1
    2
    3
    4
    5
    HzTomcat:/data # tar -xvf mysql-8.0.13-1.sles12.x86_64.rpm-bundle.tar
    HzTomcat:/data # cd mysql-8.0.13-1.sles12.x86_64/
    HzTomcat:/data/mysql-8.0.13-1.sles12.x86_64 # ls
    mysql-community-client-8.0.13-1.sles12.x86_64.rpm mysql-community-devel-8.0.13-1.sles12.x86_64.rpm mysql-community-server-8.0.13-1.sles12.x86_64.rpm
    mysql-community-common-8.0.13-1.sles12.x86_64.rpm mysql-community-libs-8.0.13-1.sles12.x86_64.rpm mysql-community-test-8.0.13-1.sles12.x86_64.rpm
  • 下面的安装要注意按顺序,先安装mysql-community-common-8.0.13-1.sles12.x86_64.rpm

    1
    2
    3
    4
    5
    HzTomcat:/data/mysql-8.0.13-1.sles12.x86_64 # rpm -ivh mysql-community-common-8.0.13-1.sles12.x86_64.rpm 
    warning: mysql-community-common-8.0.13-1.sles12.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
    Preparing... ################################# [100%]
    Updating / installing...
    1:mysql-community-common-8.0.13-1.s################################# [100%]
  • 再安装mysql-community-libs-8.0.13-1.sles12.x86_64.rpm

    1
    2
    3
    4
    5
    HzTomcat:/data/mysql-8.0.13-1.sles12.x86_64 # rpm -ivh mysql-community-libs-8.0.13-1.sles12.x86_64.rpm 
    warning: mysql-community-libs-8.0.13-1.sles12.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
    Preparing... ################################# [100%]
    Updating / installing...
    1:mysql-community-libs-8.0.13-1.sle################################# [100%]
  • 再安装mysql-community-client-8.0.13-1.sles12.x86_64.rpm

    1
    2
    3
    4
    5
    HzTomcat:/data/mysql-8.0.13-1.sles12.x86_64 # rpm -ivh mysql-community-client-8.0.13-1.sles12.x86_64.rpm 
    warning: mysql-community-client-8.0.13-1.sles12.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
    Preparing... ################################# [100%]
    Updating / installing...
    1:mysql-community-client-8.0.13-1.s################################# [100%]
  • 最后安装mysql-community-server-8.0.13-1.sles12.x86_64.rpm

    1
    2
    3
    4
    5
    HzTomcat:/data/mysql-8.0.13-1.sles12.x86_64 # rpm -ivh mysql-community-server-8.0.13-1.sles12.x86_64.rpm 
    warning: mysql-community-server-8.0.13-1.sles12.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
    Preparing... ################################# [100%]
    Updating / installing...
    1:mysql-community-server-8.0.13-1.s################################# [100%]
  • 启动

    1
    HzTomcat:~ # service mysql start
  • 查看临时root密码

    1
    2
    HzTomcat:~ # grep 'temporary password' /var/log/mysql/mysqld.log 
    2019-01-09T07:16:31.105387Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: !kwpeD_Pc7/p
  • 修改root密码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    HzTomcat:~ # mysql -uroot -p
    Enter password:
    Welcome to the MySQL monitor. Commands end with ; or \g.
    Your MySQL connection id is 8
    Server version: 8.0.13

    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> alter user 'root'@'localhost' IDENTIFIED BY 'Password123!';
    Query OK, 0 rows affected (0.14 sec)

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

    mysql> exit
    Bye

可能出现的问题

问题

在修改root密码时,忘了flush privileges,然后导致新旧密码都无法登陆的问题。

1
2
3
HzTomcat:~ # mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

解决方法

  • 修改mysql的配置文件:
1
HzTomcat:~ # vim /etc/my.cnf

增加:skip-grant-tables

  • 然后在登陆直接使用root登陆,重新设置密码,即可。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
HzTomcat:/var/lib/mysql # mysql -uroot 
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 8.0.13 MySQL Community Server - GPL

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> flush privileges;
Query OK, 0 rows affected (0.02 sec)

mysql> alter user 'root'@'localhost' identified by 'Password123!';
Query OK, 0 rows affected (0.07 sec)

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

mysql> exit
Bye
HzTomcat:/var/lib/mysql # vim /etc/my.cnf
HzTomcat:/var/lib/mysql # service mysql restart
HzTomcat:/var/lib/mysql # mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.13 MySQL Community Server - GPL