Bing、Baidu、Google 都试了,找出来的结果感觉都讲的不清不楚的,最后还是照着官方的Guide装好了,所以就记录一下。
这里我们使用Yum来安装MySQL
参考文档的位置在
A Quick Guide to Using the MySQL Yum Repository
过程大概分这么几步——
- 从官方网站的MySQL存储库下载页面,下载适用于你的操作系统的
rpm
包 - 在centOS中安装发行包文件
- 使用
yum
命令安装Mysql - 启动MySQL并查看状态
- 查看MySQL的默认root密码并修改
下载
下载连接戳
适用于centOS7的包选择这个描述的Red Hat Enterprise Linux 7 / Oracle Linux 7 (Architecture Independent), RPM Package
,包文件名应该是mysql80-community-release-el7-2.noarch.rpm
如果你的centOS可以联网,你可以直接用wget
获取这个包
wget https://dev.mysql.com/get/mysql80-community-release-el7-2.noarch.rpm --no-check-certificate
P.S. centOS 7的 core 版本里默认没有wget
,需要安装
P.S. 非root
权限记得加sudo
yum install wget -y
查看wget
下载到的文件,根目录直接执行ls
命令
[root@localhost ~]# ls
mysql80-community-release-el7-2.noarch.rpm
然后执行这条命令
rpm -Uvh mysql80-community-release-el7-2.noarch.rpm
安装
[root@localhost ~]# yum install mysql-community-server -y
大概装了这么些个东西——
# yum history info 6
Loaded plugins: fastestmirror
Transaction ID : 6
Begin time : Fri Feb 10 02:57:13 2012
Begin rpmdb : 317:58603e528d486c5243a950bd2961d1439b1ce4dd
End time : 02:59:33 2012 (140 seconds)
End rpmdb : 349:d5c5be43911eb18d2765ecdc6b3e3bb3ef199e60
User : root <root>
Return-Code : Success
Command Line : install mysql-community-server
Transaction performed with:
Installed rpm-4.11.3-35.el7.x86_64 @anaconda
Installed yum-3.4.3-161.el7.centos.noarch @anaconda
Installed yum-plugin-fastestmirror-1.1.31-50.el7.noarch @anaconda
Packages Altered:
Obsoleted mariadb-libs-1:5.5.60-1.el7_5.x86_64 @anaconda
Dep-Install mysql-community-client-8.0.15-1.el7.x86_64 @mysql80-community
Dep-Install mysql-community-common-8.0.15-1.el7.x86_64 @mysql80-community
Obsoleting mysql-community-libs-8.0.15-1.el7.x86_64 @mysql80-community
Obsoleting mysql-community-libs-compat-8.0.15-1.el7.x86_64 @mysql80-community
Install mysql-community-server-8.0.15-1.el7.x86_64 @mysql80-community
Dep-Install net-tools-2.0-0.24.20131004git.el7.x86_64 @base
Dep-Install perl-4:5.16.3-294.el7_6.x86_64 @updates
Dep-Install perl-Carp-1.26-244.el7.noarch @base
Dep-Install perl-Encode-2.51-7.el7.x86_64 @base
Dep-Install perl-Exporter-5.68-3.el7.noarch @base
Dep-Install perl-File-Path-2.09-2.el7.noarch @base
Dep-Install perl-File-Temp-0.23.01-3.el7.noarch @base
Dep-Install perl-Filter-1.49-3.el7.x86_64 @base
Dep-Install perl-Getopt-Long-2.40-3.el7.noarch @base
Dep-Install perl-HTTP-Tiny-0.033-3.el7.noarch @base
Dep-Install perl-PathTools-3.40-5.el7.x86_64 @base
Dep-Install perl-Pod-Escapes-1:1.04-294.el7_6.noarch @updates
Dep-Install perl-Pod-Perldoc-3.20-4.el7.noarch @base
Dep-Install perl-Pod-Simple-1:3.28-4.el7.noarch @base
Dep-Install perl-Pod-Usage-1.63-3.el7.noarch @base
Dep-Install perl-Scalar-List-Utils-1.27-248.el7.x86_64 @base
Dep-Install perl-Socket-2.010-4.el7.x86_64 @base
Dep-Install perl-Storable-2.45-3.el7.x86_64 @base
Dep-Install perl-Text-ParseWords-3.29-4.el7.noarch @base
Dep-Install perl-Time-HiRes-4:1.9725-3.el7.x86_64 @base
Dep-Install perl-Time-Local-1.2300-2.el7.noarch @base
Dep-Install perl-constant-1.27-2.el7.noarch @base
Dep-Install perl-libs-4:5.16.3-294.el7_6.x86_64 @updates
Dep-Install perl-macros-4:5.16.3-294.el7_6.x86_64 @updates
Dep-Install perl-parent-1:0.225-244.el7.noarch @base
Dep-Install perl-podlators-2.5.1-3.el7.noarch @base
Dep-Install perl-threads-1.87-4.el7.x86_64 @base
Dep-Install perl-threads-shared-1.43-6.el7.x86_64 @base
history info
启动 Mysql 并查看状态
用到两条命令systemctl start mysqld.service
和systemctl status mysqld.service
[root@localhost ~]# systemctl start mysqld.service
[root@localhost ~]# systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2012-02-10 06:23:03 CST; 1h 22min ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 5138 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 5207 (mysqld)
Status: "SERVER_OPERATING"
CGroup: /system.slice/mysqld.service
└─5207 /usr/sbin/mysqld
Feb 10 06:22:49 localhost.localdomain systemd[1]: Starting MySQL Server...
Feb 10 06:23:03 localhost.localdomain systemd[1]: Started MySQL Server.
查看默认生成的 root 密码并修改
官网的说法是——
At the initial start up of the server, the following happens, given that the data directory of the server is empty:
The server is initialized.
An SSL certificate and key files are generated in the data directory.
The
validate_password plugin is installed and enabled.A superuser account
'root'@'localhost'
is created. A password for the superuser is set and stored in the error log file. To reveal it, use the following command:
按我们如上的操作,data directory
毫无疑问会是空的,所以,我们应该可以看到error log file
中记录的超级用户,也就是'root'@'localhost'
的密码
[root@localhost ~]# grep 'temporary password' /var/log/mysqld.log
2012-02-09T22:22:55.825703Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 9)lm0moC+flL
密码是9)lm0moC+flL
,由于可以通过这个log文件获取到,肯定是不安全的,需要赶紧改……
[root@localhost ~]# mysql -uroot -p
Enter password:
使用系统生成的密码登陆,看到下面的输出就对了——
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.15 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> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';
这里,MyNewPass4!
就是改后的密码,这只是个示例;
末尾一定要加;
或者/g
,否则命令是不会执行的;
Yum 库安装 Mysql 的注意事项
官方原文是这么写的——
Once the release package is installed on your system, any system-wide update by the
yum update command (or
dnf upgrade for dnf-enabled systems) will automatically upgrade MySQL packages on your system and also replace any native third-party packages, if Yum finds replacements for them in the MySQL Yum repository
所以,一旦安装了Yum发行包,就会通过yum update
命令来更新 MySQL 以及所有相关的依赖,大概是这么个意思……
安装额外的 Mysql 组件
如果于到需要安装一些额外的 MySQL 组件,需要这样子——
列出当前平台所支持的 MySQL 组件
[root@localhost ~]# yum --disablerepo=\* --enablerepo='mysql*-community*' list available
可以看到这样的输出
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Available Packages
mysql-cluster-community-auto-installer.x86_64 7.6.9-1.el7 mysql-cluster-7.6-community
mysql-cluster-community-client.x86_64 7.6.9-1.el7 mysql-cluster-7.6-community
mysql-cluster-community-common.x86_64 7.6.9-1.el7 mysql-cluster-7.6-community
mysql-cluster-community-data-node.x86_64 7.6.9-1.el7 mysql-cluster-7.6-community
mysql-cluster-community-devel.x86_64 7.6.9-1.el7 mysql-cluster-7.6-community
mysql-cluster-community-embedded.x86_64 7.6.9-1.el7 mysql-cluster-7.6-community
mysql-cluster-community-embedded-compat.x86_64 7.6.9-1.el7 mysql-cluster-7.6-community
mysql-cluster-community-embedded-devel.x86_64 7.6.9-1.el7 mysql-cluster-7.6-community
mysql-cluster-community-java.x86_64 7.6.9-1.el7 mysql-cluster-7.6-community
mysql-cluster-community-libs.x86_64 7.6.9-1.el7 mysql-cluster-7.6-community
mysql-cluster-community-libs-compat.x86_64 7.6.9-1.el7 mysql-cluster-7.6-community
mysql-cluster-community-management-server.x86_64 7.6.9-1.el7 mysql-cluster-7.6-community
mysql-cluster-community-memcached.x86_64 7.6.9-1.el7 mysql-cluster-7.6-community
mysql-cluster-community-ndbclient.x86_64 7.6.9-1.el7 mysql-cluster-7.6-community
mysql-cluster-community-ndbclient-devel.x86_64 7.6.9-1.el7 mysql-cluster-7.6-community
mysql-cluster-community-nodejs.x86_64 7.6.9-1.el7 mysql-cluster-7.6-community
mysql-cluster-community-server.x86_64 7.6.9-1.el7 mysql-cluster-7.6-community
mysql-cluster-community-test.x86_64 7.6.9-1.el7 mysql-cluster-7.6-community
mysql-community-bench.x86_64 5.6.43-2.el7 mysql56-community
mysql-community-client.i686 8.0.15-1.el7 mysql80-community
mysql-community-common.i686 8.0.15-1.el7 mysql80-community
mysql-community-devel.i686 8.0.15-1.el7 mysql80-community
mysql-community-devel.x86_64 8.0.15-1.el7 mysql80-community
mysql-community-embedded.i686 5.7.25-1.el7 mysql57-community
mysql-community-embedded.x86_64 5.7.25-1.el7 mysql57-community
mysql-community-embedded-compat.i686 8.0.15-1.el7 mysql80-community
mysql-community-embedded-compat.x86_64 8.0.15-1.el7 mysql80-community
mysql-community-embedded-devel.i686 5.7.25-1.el7 mysql57-community
mysql-community-embedded-devel.x86_64 5.7.25-1.el7 mysql57-community
mysql-community-libs.i686 8.0.15-1.el7 mysql80-community
mysql-community-libs-compat.i686 8.0.15-1.el7 mysql80-community
mysql-community-release.noarch el7-7 mysql57-community
mysql-community-test.x86_64 8.0.15-1.el7 mysql80-community
mysql-connector-c++.x86_64 8.0.15-1.el7 mysql-connectors-community
mysql-connector-c++-devel.x86_64 8.0.15-1.el7 mysql-connectors-community
mysql-connector-c++-jdbc.x86_64 8.0.15-1.el7 mysql-connectors-community
mysql-connector-java.noarch 8.0.15-1.el7 mysql-connectors-community
mysql-connector-odbc.x86_64 8.0.15-1.el7 mysql-connectors-community
mysql-connector-odbc-debuginfo.x86_64 8.0.15-1.el7 mysql-connectors-community
mysql-connector-odbc-setup.x86_64 8.0.15-1.el7 mysql-connectors-community
mysql-connector-python.noarch 2.0.4-1.el7 mysql-connectors-community
mysql-connector-python.x86_64 8.0.15-1.el7 mysql-connectors-community
mysql-connector-python-cext.x86_64 8.0.15-1.el7 mysql-connectors-community
mysql-connector-python-debuginfo.x86_64 2.1.7-1.el7 mysql-connectors-community
mysql-ref-manual-5.5-en-html-chapter.noarch 1-20180830 mysql55-community
mysql-ref-manual-5.5-en-pdf.noarch 1-20180830 mysql55-community
mysql-ref-manual-5.6-en-html-chapter.noarch 1-20181222 mysql56-community
mysql-ref-manual-5.6-en-pdf.noarch 1-20181222 mysql56-community
mysql-ref-manual-5.7-en-html-chapter.noarch 1-20181224 mysql57-community
mysql-ref-manual-5.7-en-pdf.noarch 1-20181224 mysql57-community
mysql-ref-manual-8.0-en-html-chapter.noarch 1-20190127 mysql80-community
mysql-ref-manual-8.0-en-pdf.noarch 1-20190127 mysql80-community
mysql-router.x86_64 8.0.12-1.el7 mysql-tools-community
mysql-router-community.x86_64 8.0.15-1.el7 mysql-tools-community
mysql-router-debuginfo.x86_64 8.0.12-1.el7 mysql-tools-community
mysql-shell.x86_64 8.0.15-1.el7 mysql-tools-community
mysql-shell-debuginfo.x86_64 8.0.15-1.el7 mysql-tools-community
mysql-utilities.noarch 1.6.5-1.el7 mysql-tools-community
mysql-utilities-extra.noarch 1.5.6-1.el7 mysql-tools-community
mysql-workbench-community.x86_64 8.0.15-1.el7 mysql-tools-community
mysql-workbench-community-debuginfo.x86_64 8.0.15-1.el7 mysql-tools-community
mysql57-community-release.noarch el7-10 mysql-cluster-7.5-community
如果要安装哪个包,就这样——
yum install *package-name*
*package-name*
替换成实际要安装的包的名字
使用MySQL命令行中的帮助
在mysql>
命令行提示符的状态下,输入help
或者\h
调出的是基本的命令
mysql> help
For information about MySQL products and services, visit:
http://www.mysql.com/
For developer information, including the MySQL Reference Manual, visit:
http://dev.mysql.com/
To buy MySQL Enterprise support, training, or other products, visit:
https://shop.mysql.com/
List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
? (\?) Synonym for `help'.
clear (\c) Clear the current input statement.
connect (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
edit (\e) Edit command with $EDITOR.
ego (\G) Send command to mysql server, display result vertically.
exit (\q) Exit mysql. Same as quit.
go (\g) Send command to mysql server.
help (\h) Display this help.
nopager (\n) Disable pager, print to stdout.
notee (\t) Don't write into outfile.
pager (\P) Set PAGER [to_pager]. Print the query results via PAGER.
print (\p) Print current command.
prompt (\R) Change your mysql prompt.
quit (\q) Quit mysql.
rehash (\#) Rebuild completion hash.
source (\.) Execute an SQL script file. Takes a file name as an argument.
status (\s) Get status information from the server.
system (\!) Execute a system shell command.
tee (\T) Set outfile [to_outfile]. Append everything into given outfile.
use (\u) Use another database. Takes database name as argument.
charset (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.
resetconnection(\x) Clean session context.
For server side help, type 'help contents'
mysql>
输入help contents
可以调出“真正地”帮助
mysql> help contents
You asked for help about help category: "Contents"
For more information, type 'help <item>', where <item> is one of the following
categories:
Account Management
Administration
Components
Compound Statements
Data Definition
Data Manipulation
Data Types
Functions
Functions and Modifiers for Use with GROUP BY
Geographic Features
Help Metadata
Language Structure
Plugins
Storage Engines
Table Maintenance
Transactions
User-Defined Functions
Utility
mysql>
这个列表里有很多分类,按照提示,如果要看具体的分类下面的命令就要输入help <item>
,比如我要看和数据操作有关的,就要输入——
mysql> help Data Manipulation
然后就会看到这个——
You asked for help about help category: "Data Manipulation"
For more information, type 'help <item>', where <item> is one of the following
topics:
CALL
DELETE
DO
DUAL
HANDLER
IMPORT TABLE
INSERT
INSERT DELAYED
INSERT SELECT
JOIN
LOAD DATA
LOAD XML
REPLACE
SELECT
UNION
UPDATE
进一步我们输入help SELECT
mysql> help SELECT
Name: 'SELECT'
Description:
Syntax:
SELECT
[ALL | DISTINCT | DISTINCTROW ]
[HIGH_PRIORITY]
[STRAIGHT_JOIN]
[SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
SQL_NO_CACHE [SQL_CALC_FOUND_ROWS]
select_expr [, select_expr ...]
[FROM table_references
[PARTITION partition_list]
[WHERE where_condition]
[GROUP BY {col_name | expr | position}, ... [WITH ROLLUP]]
[HAVING where_condition]
[WINDOW window_name AS (window_spec)
[, window_name AS (window_spec)] ...]
[ORDER BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
[INTO OUTFILE 'file_name'
[CHARACTER SET charset_name]
export_options
| INTO DUMPFILE 'file_name'
| INTO var_name [, var_name]]
[FOR {UPDATE | SHARE} [OF tbl_name [, tbl_name] ...] [NOWAIT | SKIP LOCKED]
| LOCK IN SHARE MODE]]
SELECT is used to retrieve rows selected from one or more tables, and
can include UNION statements and subqueries. See [HELP UNION], and
http://dev.mysql.com/doc/refman/8.0/en/subqueries.html. A SELECT
statement can start with a WITH clause to define common table
expressions accessible within the SELECT. See
http://dev.mysql.com/doc/refman/8.0/en/with.html.
The most commonly used clauses of SELECT statements are these:
o Each select_expr indicates a column that you want to retrieve. There
must be at least one select_expr.
o table_references indicates the table or tables from which to retrieve
rows. Its syntax is described in [HELP JOIN].
o SELECT supports explicit partition selection using the PARTITION with
a list of partitions or subpartitions (or both) following the name of
the table in a table_reference (see [HELP JOIN]). In this case, rows
are selected only from the partitions listed, and any other
partitions of the table are ignored. For more information and
examples, see
http://dev.mysql.com/doc/refman/8.0/en/partitioning-selection.html.
SELECT ... PARTITION from tables using storage engines such as MyISAM
that perform table-level locks (and thus partition locks) lock only
the partitions or subpartitions named by the PARTITION option.
For more information, see Partitioning and Locking
(http://dev.mysql.com/doc/refman/5.7/en/partitioning-limitations-lock
ing.html).
o The WHERE clause, if given, indicates the condition or conditions
that rows must satisfy to be selected. where_condition is an
expression that evaluates to true for each row to be selected. The
statement selects all rows if there is no WHERE clause.
In the WHERE expression, you can use any of the functions and
operators that MySQL supports, except for aggregate (summary)
functions. See
http://dev.mysql.com/doc/refman/8.0/en/expressions.html, and
http://dev.mysql.com/doc/refman/8.0/en/functions.html.
SELECT can also be used to retrieve rows computed without reference to
any table.
URL: http://dev.mysql.com/doc/refman/8.0/en/select.html
mysql>
emmmmmm……只看这个并看不懂SELECT的用法,不过如果忘了命令,用来临时找一下还好
所以,如果有一个SQL语句后面能接什么不记得了,就用命令行程序自带的help来看就好了……
e.g. CREATE——
mysql> help CREATE
Many help items for your request exist.
To make a more specific request, please type 'help <item>',
where <item> is one of the following
topics:
CREATE DATABASE
CREATE EVENT
CREATE FUNCTION
CREATE FUNCTION UDF
CREATE INDEX
CREATE PROCEDURE
CREATE RESOURCE GROUP
CREATE ROLE
CREATE SERVER
CREATE SPATIAL REFERENCE SYSTEM
CREATE TABLE
CREATE TABLESPACE
CREATE TRIGGER
CREATE USER
CREATE VIEW
SHOW
SHOW CREATE DATABASE
SHOW CREATE EVENT
SHOW CREATE FUNCTION
SHOW CREATE PROCEDURE
SHOW CREATE TABLE
SHOW CREATE USER
SPATIAL
mysql> help CREATE DATABASE
Name: 'CREATE DATABASE'
Description:
Syntax:
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
[create_specification] ...
create_specification:
[DEFAULT] CHARACTER SET [=] charset_name
| [DEFAULT] COLLATE [=] collation_name
CREATE DATABASE creates a database with the given name. To use this
statement, you need the CREATE privilege for the database. CREATE
SCHEMA is a synonym for CREATE DATABASE.
URL: http://dev.mysql.com/doc/refman/8.0/en/create-database.html
mysql>
P.S. 一开始找了半天没找到帮助怎么调出来……