背景
每次重搭环境都免不了要重新安装数据库,虽然频率不高,也发现竟然在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 | [mysqld] |
- 以管理员身份运行cmd,进入到%MYSQL_HOME%到bin目录下运行如下命令:
1 | mysqld --initialize -insecure # 初始化mysql,创建root用户,密码为空 |
最后提示: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 | MacBook-Air:~ icbc$ alias mysql=/usr/local/mysql/bin/mysql |
注意:这种方式只能在当前命令行中有效。
- 建议是在环境变量中增加:
1 | cd ~ |
- 进行root密码重置,比如重置为root,运行如下命令,然后输入临时密码即可。
1 | MacBook-Air:~ icbc$ mysqladmin -u root -p password root |
Suse下离线安装MySQL
先下载Suse版本的RPM包,官网有很多,要下载
Bundle
版,否则得分别下载各个组件。SUSE Linux Enterprise Server 12 (x86, 64-bit), RPM Bundle
解压安装包
1
2
3
4
5HzTomcat:/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
5HzTomcat:/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
5HzTomcat:/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
5HzTomcat:/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
5HzTomcat:/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
2HzTomcat:~ # 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
22HzTomcat:~ # 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 | HzTomcat:~ # mysql -uroot -p |
解决方法
- 修改
mysql
的配置文件:
1 | HzTomcat:~ # vim /etc/my.cnf |
增加:skip-grant-tables
- 然后在登陆直接使用
root
登陆,重新设置密码,即可。
1 | HzTomcat:/var/lib/mysql # mysql -uroot |