欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

centos7.4安装mysql的步骤和常用语句详解

程序员文章站 2023-08-26 08:45:42
1)检查该机器上是否安装过mysql。 rpm -qa|grep -i mysql #忽略大小写查找mysql 2)下载rpm安装包。 [root@izrj932jby4d2dfv1nj3x6z h...

1)检查该机器上是否安装过mysql

rpm -qa|grep -i mysql #忽略大小写查找mysql

2)下载rpm安装包。

[root@izrj932jby4d2dfv1nj3x6z html]# wget https://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

[root@izrj932jby4d2dfv1nj3x6z java]# rpm -ivh mysql-community-release-el7-5.noarch.rpm

3)安装mysql。

[root@izrj932jby4d2dfv1nj3x6z java]# yum install mysql-server

4)检验mysql。

[root@izrj932jby4d2dfv1nj3x6z java]# mysql -v #查看版本

mysql ver 14.14 distrib 5.6.40, for linux (x86_64) using editline wrapper

[root@izrj932jby4d2dfv1nj3x6z java]# mysql -u root #登录mysql监视器mysql monitor

error 2002 (hy000): can't connect to local mysql server through socket '/var/lib/mysql/mysql.sock' (2)

[root@izrj932jby4d2dfv1nj3x6z lib]# chown -r root:root /var/lib/mysql #设置拥有者,重启mysql服务就可以登录成功。

[root@izrj932jby4d2dfv1nj3x6z lib]# service mysqld restart

redirecting to /bin/systemctl restart mysqld.service

mysql> use mysql #切换为mysql

reading table information for completion of table and column names

you can turn off this feature to get a quicker startup with -a

database changed

mysql> update user set password=password('12345678') where user='root'; #修改root密码,重启mysql服务后生效。

query ok, 4 rows affected (0.00 sec)

rows matched: 4 changed: 4 warnings: 0

mysql> exit;

bye

mysql> show databases; #显示所有数据库

+--------------------+

| database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

+--------------------+

3 rows in set (0.00 sec)

mysql> show tables; #显示所有表

mysql> desc user; #显示表字段定义

mysql> show create table slow_log\g; #显示建表语句

mysql> select user() #查看当前用户

mysql> select database(); #查看当前所在数据库

mysql> select version(); #查看数据库版本

mysql> show status; #显示状态

mysql> show global status; #全局数据库状态

mysql> show slave status\g; #查看主从数据库状态信息

mysql> show variables; #显示参数

mysql> show variables like 'max_connect%';

+--------------------+-------+

| variable_name | value |

+--------------------+-------+

| max_connect_errors | 100 |

| max_connections | 151 |

+--------------------+-------+

2 rows in set (0.00 sec)

mysql> set global max_connect_errors =1000; #修改数据库参数,重启数据库会失效,要在配置文件中修改。

query ok, 0 rows affected (0.00 sec)

mysql> show variables like 'max_connect%';

+--------------------+-------+

| variable_name | value |

+--------------------+-------+

| max_connect_errors | 1000 |

| max_connections | 151 |

+--------------------+-------+

2 rows in set (0.00 sec)

mysql> create database bigdata charset utf8; #创建数据库

mysql> create table t_user(id int primary key auto_increment, name char(40), sex char(1), loginname char(40), nickname char(400) ); #创建表

mysql> alter table t_user change nickname password varchar(40); #修改字段名

mysql> alter table t_user add create_time timestamp not null default current_timestamp; #新增字段

mysql> alter table t_user add update_time timestamp not null default current_timestamp on update current_timestamp;

mysql> alter table t_user modify column update_time2 decimal(10,1) default null comment '测试字段'; #修改字段类型

mysql> alter table t_user drop update_time2; #删除字段

mysql> select * from t_user; #查询记录

mysql> insert into t_user(name, sex, loginname, password) values('tianci','0','admin','123456'); #插入记录

mysql> insert into t_user(name, sex, loginname, password) values('tianci','0','admin','123456'),('qingshan','1','demo','123456');

mysql> update t_user set name='tianci2' where tid=2; #修改记录

mysql> delete from t_user where id=2; #删除字段

mysql> truncate table t_user; #清空表数据

mysql> alter table t_user add index idx_loginname(loginname); #添加索引

mysql> alter table t_user add primary key(id); #添加主键索引

mysql> alter table t_user add unique unique_name(name); #添加唯一索引

mysql> alter table t_user drop index idx_loginname; #删除索引

mysql> drop table t_user; #删除表

mysql> drop database bigdata; #删除数据库

[root@izrj932jby4d2dfv1nj3x6z lib]# mysqldump -uroot -p'123456' bigdata>/tmp/bigdata.sql #备份数据库

[root@izrj932jby4d2dfv1nj3x6z lib]# mysql -uroot -p bigdata

mysql> create user 'test_admin'@'%' identified by 'password'; #创建用户test_admin密码为password,可任何电脑登录。

mysql> create user 'test_admin2'@'localhost' identified by '';

mysql> show grants for test_admin; #显示用户test_admin的权限

+-----------------------------------------------------------------------------------------------------------+

| grants for test_admin@% |

+-----------------------------------------------------------------------------------------------------------+

| grant usage on *.* to 'test_admin'@'%' identified by password '*2470c0c06dee42fd1618bb99005adca2ec9d1e19' |

+-----------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

mysql> grant select on bigdata.* to 'test_admin2'@'localhost' identified by 'a123456' with grant option; #授予用户test_admin2,数据库bigdata的select权限,可以为其他用户分配权限。

mysql> flush privileges; #另外每当调整权限后,通常需要执行以下语句刷新权限

mysql> revoke all on *.* from 'test_admin2'@'localhost'; #撤销权限

mysql> flush privileges;

mysql> source batch.sql; #批量执行sql,即使中间出错,后续脚本也会继续执行。

[root@izrj932jby4d2dfv1nj3x6z lib]# mysql -u root -p -e"use bigdata; select * from t_user;select 1+2+3+4+5+6;"

enter password:

+-------------+

| 1+2+3+4+5+6 |

+-------------+

| 21 |

+-------------+

[root@izrj932jby4d2dfv1nj3x6z lib]# mysql -u root -p < > use bigdata;

> select * from t_user;

> select 1 ;

> eof

grant syntax:https://dev.mysql.com/doc/refman/5.6/en/grant.html

--------------------------------------------------------------------------------

登录mysql后的命令帮助

--------------------------------------------------------------------------------

mysql> help

for information about mysql products and services, visit:

https://www.mysql.com/

for developer information, including the mysql reference manual, visit:

https://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.

for server side help, type 'help contents'

mysql>

--------------------------------------------------------------------------------

mysql程序的参数

--------------------------------------------------------------------------------

[root@izrj932jby4d2dfv1nj3x6z lib]# mysql --help

mysql ver 14.14 distrib 5.6.40, for linux (x86_64) using editline wrapper

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.

usage: mysql [options] [database]

-, --help display this help and exit.

-i, --help synonym for -

--auto-rehash enable automatic rehashing. one doesn't need to use

'rehash' to get table and field completion, but startup

and reconnecting may take a longer time. disable with

--disable-auto-rehash.

(defaults to on; use --skip-auto-rehash to disable.)

-a, --no-auto-rehash

no automatic rehashing. one has to use 'rehash' to get

table and field completion. this gives a quicker start of

mysql and disables rehashing on reconnect.

--auto-vertical-output

automatically switch to vertical output mode if the

result is wider than the terminal width.

-b, --batch don't use history file. disable interactive behavior.

(enables --silent.)

--bind-address=name ip address to bind to.

-b, --binary-as-hex print binary data as hex

--character-sets-dir=name

directory for character set files.

--column-type-info display column type information.

-c, --comments preserve comments. send comments to the server. the

default is --skip-comments (discard comments), enable

with --comments.

-c, --compress use compression in server/client protocol.

-#, --debug[=#] this is a non-debug version. catch this and exit.

--debug-check check memory and open file usage at exit.

-t, --debug-info print some debug info at exit.

-d, --database=name database to use.

--default-character-set=name

set the default character set.

--delimiter=name delimiter to be used.

--enable-cleartext-plugin

enable/disable the clear text authentication plugin.

-e, --execute=name execute command and quit. (disables --force and history

file.)

-e, --vertical print the output of a query (rows) vertically.

-f, --force continue even if we get an sql error.

-g, --named-commands

enable named commands. named commands mean this program's

internal commands; see mysql> help . when enabled, the

named commands can be used from any line of the query,

otherwise only from the first line, before an enter.

disable with --disable-named-commands. this option is

disabled by default.

-i, --ignore-spaces ignore space after function names.

--init-command=name sql command to execute when connecting to mysql server.

will automatically be re-executed when reconnecting.

--local-infile enable/disable load data local infile.

-b, --no-beep turn off beep on error.

-h, --host=name connect to host.

-h, --html produce html output.

-x, --xml produce xml output.

--line-numbers write line numbers for errors.

(defaults to on; use --skip-line-numbers to disable.)

-l, --skip-line-numbers

don't write line number for errors.

-n, --unbuffered flush buffer after each query.

--column-names write column names in results.

(defaults to on; use --skip-column-names to disable.)

-n, --skip-column-names

don't write column names in results.

--sigint-ignore ignore sigint (ctrl-c).

-o, --one-database ignore statements except those that occur while the

default database is the one named at the command line.

--pager[=name] pager to use to display results. if you don't supply an

option, the default pager is taken from your env variable

pager. valid pagers are less, more, cat [> filename],

etc. see interactive help (\h) also. this option does not

work in batch mode. disable with --disable-pager. this

option is disabled by default.

-p, --password[=name]

password to use when connecting to server. if password is

not given it's asked from the tty.

-p, --port=# port number to use for connection or 0 for default to, in

order of preference, my.cnf, $mysql_tcp_port,

/etc/services, built-in default (3306).

--prompt=name set the mysql prompt to this value.

--protocol=name the protocol to use for connection (tcp, socket, pipe,

memory).

-q, --quick don't cache result, print it row by row. this may slow

down the server if the output is suspended. doesn't use

history file.

-r, --raw write fields without conversion. used with --batch.

--reconnect reconnect if the connection is lost. disable with

--disable-reconnect. this option is enabled by default.

(defaults to on; use --skip-reconnect to disable.)

-s, --silent be more silent. print results with a tab as separator,

each row on new line.

-s, --socket=name the socket file to use for connection.

--ssl enable ssl for connection (automatically enabled with

other flags).

--ssl-ca=name ca file in pem format (check openssl docs, implies

--ssl).

--ssl-capath=name ca directory (check openssl docs, implies --ssl).

--ssl-cert=name x509 cert in pem format (implies --ssl).

--ssl-cipher=name ssl cipher to use (implies --ssl).

--ssl-key=name x509 key in pem format (implies --ssl).

--ssl-crl=name certificate revocation list (implies --ssl).

--ssl-crlpath=name certificate revocation list path (implies --ssl).

--ssl-verify-server-cert

verify server's "common name" in its cert against

hostname used when connecting. this option is disabled by

default.

--ssl-mode=name ssl connection mode.

-t, --table output in table format.

--tee=name append everything into outfile. see interactive help (\h)

also. does not work in batch mode. disable with

--disable-tee. this option is disabled by default.

-u, --user=name user for login if not current user.

-u, --safe-updates only allow update and delete that uses keys.

-u, --i-am-a-dummy synonym for option --safe-updates, -u.

-v, --verbose write more. (-v -v -v gives the table output format).

-v, --version output version information and exit.

-w, --wait wait and retry if connection is down.

--connect-timeout=# number of seconds before connection timeout.

--max-allowed-packet=#

the maximum packet length to send to or receive from

server.

--net-buffer-length=#

the buffer size for tcp/ip and socket communication.

--select-limit=# automatic limit for select when using --safe-updates.

--max-join-size=# automatic limit for rows in a join when using

--safe-updates.

--secure-auth refuse client connecting to server if it uses old

(pre-4.1.1) protocol.

(defaults to on; use --skip-secure-auth to disable.)

--server-arg=name send embedded server this as a parameter.

--show-warnings show warnings after every statement.

--plugin-dir=name directory for client-side plugins.

--default-auth=name default authentication client-side plugin to use.

--histignore=name a colon-separated list of patterns to keep statements

from getting logged into mysql history.

--binary-mode by default, ascii '\0' is disallowed and '\r\n' is

translated to '\n'. this switch turns off both features,

and also turns off parsing of all clientcommands except

\c and delimiter, in non-interactive mode (for input

piped to mysql or loaded using the 'source' command).

this is necessary when processing output from mysqlbinlog

that may contain blobs.

--connect-expired-password

notify the server that this client is prepared to handle

expired password sandbox mode.

default options are read from the following files in the given order:

/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf

the following groups are read: mysql client

the following options may be given as the first argument:

--print-defaults print the program argument list and exit.

--no-defaults don't read default options from any option file,

except for login file.

--defaults-file=# only read default options from the given file #.

--defaults-extra-file=# read this file after the global files are read.

--defaults-group-suffix=#

also read groups with concat(group, suffix)

--login-path=# read this path from the login file.

variables (--variable-name=value)

and boolean options {false|true} value (after reading options)

--------------------------------- ----------------------------------------

auto-rehash true

auto-vertical-output false

bind-address (no default value)

binary-as-hex false

character-sets-dir (no default value)

column-type-info false

comments false

compress false

debug-check false

debug-info false

database (no default value)

default-character-set auto

delimiter ;

enable-cleartext-plugin false

vertical false

force false

named-commands false

ignore-spaces false

init-command (no default value)

local-infile false

no-beep false

host (no default value)

html false

xml false

line-numbers true

unbuffered false

column-names true

sigint-ignore false

port 0

prompt mysql>

quick false

raw false

reconnect true

socket (no default value)

ssl false

ssl-ca (no default value)

ssl-capath (no default value)

ssl-cert (no default value)

ssl-cipher (no default value)

ssl-key (no default value)

ssl-crl (no default value)

ssl-crlpath (no default value)

ssl-verify-server-cert false

table false

user (no default value)

safe-updates false

i-am-a-dummy false

connect-timeout 0

max-allowed-packet 16777216

net-buffer-length 16384

select-limit 1000

max-join-size 1000000

secure-auth true

show-warnings false

plugin-dir (no default value)

default-auth (no default value)

histignore (no default value)

binary-mode false

connect-expired-password false

[root@izrj932jby4d2dfv1nj3x6z lib]#

--------------------------------------------------------------------------------