前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux CentOS7 (Windows)系统安装 MySQL

Linux CentOS7 (Windows)系统安装 MySQL

作者头像
zoro
发布2019-04-11 15:31:52
7020
发布2019-04-11 15:31:52
举报
文章被收录于专栏:Java开发者Java开发者

Linux CentOS7 (Windows)系统安装 MySQL

MySQL 是最流行的关系型数据库管理系统,在 WEB 应用方面 MySQL 是最好的 RDBMS(Relational Database Management System:关系数据库管理系统)应用软件之一。

优秀博客: https://blog.csdn.net/jubincn/article/details/6725582 http://www.runoob.com/mysql/mysql-install.html

Navicat Premium 12.1.12.0安装与激活: https://www.jianshu.com/p/5f693b4c9468


MySQL 安装

检测系统是否自带安装 MySQL:

代码语言:javascript
复制
rpm -qa | grep mysql

如果你系统有安装,那可以选择进行卸载:

代码语言:javascript
复制
rpm -e mysql  // 普通删除模式
rpm -e --nodeps mysql  // 强力删除模式,如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对其进行强力删除

安装 MySQL: yum 资源包:https://dev.mysql.com/downloads/repo/yum/

代码语言:javascript
复制
[root@localhost software]# wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
[root@localhost software]# rpm -ivh [root@localhost software]# mysql-community-release-el7-5.noarch.rpm
[root@localhost software]# yum update
[root@localhost software]# yum install mysql-server

权限设置:

代码语言:javascript
复制
[root@localhost software]# chown mysql:mysql -R /var/lib/mysql

初始化 MySQL:

代码语言:javascript
复制
[root@localhost software]# mysqld --initialize

注意:

可能会出现以下错误提示

代码语言:javascript
复制
Please read "Security" section of the manual to find out how to run mysqld as root!

原因:

Mysql的解释是:永远不要使用root帐号启动MySQL Server。这样做很危险,因为拥有FILE'权限的用户会使得MySQL Server使用root帐户创建文件(比如,~root/.bashrc),为了防止类似的事情发生,mysqld默认拒绝用户使用root帐户启动,但root用户可以通过在命令后面加上"--user=root"选项来强行启动mysqld。

解决方法:

代码语言:javascript
复制
[root@localhost software]# vim /etc/my.cnf

新增:

代码语言:javascript
复制
user=mysql #或者user=root

启动 MySQL:

代码语言:javascript
复制
[root@localhost software]# systemctl start mysqld

查看 MySQL 运行状态:

代码语言:javascript
复制
[root@localhost software]# systemctl status mysqld

   mysqld.service - MySQL Community Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since 五 2019-01-25 21:20:53 CST; 35s ago
  Process: 66362 ExecStartPost=/usr/bin/mysql-systemd-start post (code=exited, status=0/SUCCESS)
  Process: 66300 ExecStartPre=/usr/bin/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
 Main PID: 66360 (mysqld_safe)
   CGroup: /system.slice/mysqld.service
           ├─66360 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
           └─66539 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mysqld.log --pid-file=/var/run/mysq...

1月 25 21:20:52 localhost.localdomain mysql-systemd-start[66300]: Support MySQL by buying support/licenses at http://shop.mysql.com
1月 25 21:20:52 localhost.localdomain mysql-systemd-start[66300]: Note: new default config file not created.
1月 25 21:20:52 localhost.localdomain mysql-systemd-start[66300]: Please make sure your config file is current
1月 25 21:20:52 localhost.localdomain mysql-systemd-start[66300]: WARNING: Default config file /etc/my.cnf exists on the system
1月 25 21:20:52 localhost.localdomain mysql-systemd-start[66300]: This file will be read by default by the MySQL server
1月 25 21:20:52 localhost.localdomain mysql-systemd-start[66300]: If you do not want to use this, either remove it, or use the
1月 25 21:20:52 localhost.localdomain mysql-systemd-start[66300]: --defaults-file argument to mysqld_safe when starting the server
1月 25 21:20:52 localhost.localdomain mysqld_safe[66360]: 190125 21:20:52 mysqld_safe Logging to '/var/log/mysqld.log'.
1月 25 21:20:52 localhost.localdomain mysqld_safe[66360]: 190125 21:20:52 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
1月 25 21:20:53 localhost.localdomain systemd[1]: Started MySQL Community Server.

验证 MySQL 安装; 在 linux 上目录为 /usr/bin 目录,在 Windows 为 C:\mysql\bin 。

代码语言:javascript
复制
[root@localhost software]# mysqladmin --version
 mysqladmin  Ver 8.42 Distrib 5.6.43, for Linux on x86_64
[root@localhost software]# 

登录:

代码语言:javascript
复制
[root@localhost software]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.43 MySQL Community Server (GPL)

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

可以测试一下mysql命令:

代码语言:javascript
复制
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.01 sec)

mysql> 
Mysql设置密码:

默认的root用户密码为空,以下命令来创建root用户的密码:

代码语言:javascript
复制
[root@localhost software]# mysqladmin -u root password "root";
Warning: Using a password on the command line interface can be insecure.

再次连接

代码语言:javascript
复制
[root@localhost software]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.43 MySQL Community Server (GPL)

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

注意:在输入密码时,密码是不会显示了,你正确输入即可。


Windows 上安装 MySQL

下载地址:https://dev.mysql.com/downloads/mysql/

本次解压完后放在 C:\web\mysql-8.0.11 下。

接下来我们需要配置下 MySQL 的配置文件。

在C:\web\mysql-8.0.11目录下创建 my.ini ,新增以下内容:

代码语言:javascript
复制
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
 
[mysqld]
# 设置3306端口
port = 3306
# 设置mysql的安装目录
basedir=C:\\web\\mysql-8.0.11
# 设置 mysql数据库的数据的存放目录,MySQL 8+ 不需要以下配置,系统自己生成即可,否则有可能报错
# datadir=C:\\web\\sqldata
# 允许最大连接数
max_connections=20
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB

初始化数据库:

代码语言:javascript
复制
cd C:\web\mysql-8.0.11\bin
mysqld --initialize --console

执行完成后,会有初始密码,回头会用到。 安装:

代码语言:javascript
复制
mysqld install

启动:

代码语言:javascript
复制
net start mysql

在 5.7 需要初始化 data 目录:

代码语言:javascript
复制
cd C:\web\mysql-8.0.11\bin 
mysqld --initialize-insecure 

然后再 net start mysql 。

登录 MySQL
代码语言:javascript
复制
[root@localhost software]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.6.43 MySQL Community Server (GPL)

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

输入密码就行,密码默认不显示。

MySQL 重置密码

如果忘记密码,可以在/etc/my.conf文件中在,找到 [mysqld] 之后再它下面,新增一行 skip-grant-tables (跳过密码)

代码语言:javascript
复制
skip-grant-tables

重启

代码语言:javascript
复制
service mysql restart

登录

代码语言:javascript
复制
mysql -u root -p

修改密码 :

代码语言:javascript
复制
mysql> use mysql;
mysql>  update user set authentication_string=password("123456") where user='root';
mysql> flush privileges;  # 刷新权限

或者:

代码语言:javascript
复制
service mysql stop

进入目录,以安全模式启动 MySQL

代码语言:javascript
复制
cd /usr/local/mysql/bin  
./mysqld_safe --skip-grant-tables & 

或者

代码语言:javascript
复制
update user set authentication_string=password("123456"),plugin='mysql_native_password' where user='root';

如果问题,欢迎留言:)

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.01.26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Linux CentOS7 (Windows)系统安装 MySQL
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档