选项文件:默认/etc/my.cnf
可以通过以下选项,指定选项文件:
-defaults-file:指定选项文件
例如:mysql--defaults-file=/etc/my.cnf
-no-defaults:不读任何选项文件,所有选项需要在命令行中指定
-defaults-extra-file:除了默认的选项文件之外,读该选项文件
my_print_defaults:列出所有被读到的选项文件中的选项组
my_print_defaults mysqld
mysql--print-default smysql
配置选项文件读取顺序
[root@student ~]# mysqld --help --verbose|grep my.cnf
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf
my.cnf, $MYSQL_TCP_PORT, /etc/services, built-in default
隐藏登录信息,实现免密码登录
.mylogin.cn文件:二进制,加密,位于当前操作系统用户的HOME目录
[root@student ~]# mysql_config_editor --help
mysql_config_editor Ver 8.0.13 for linux-glibc2.12 on x86_64 (MySQL Community Server - GPL)
Copyright (c) 2012, 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.
MySQL Configuration Utility.
Usage: mysql_config_editor [program options] [command [command options]]
-#, --debug[=#] This is a non-debug version. Catch this and exit.
-?, --help Display this help and exit.
-v, --verbose Write more information.
-V, --version Output version information and exit.
Variables (--variable-name=value)
and boolean options {FALSE|TRUE} Value (after reading options)
--------------------------------- ----------------------------------------
verbose FALSE
Where command can be any one of the following :
set [command options] Sets user name/password/host name/socket/port
for a given login path (section).
remove [command options] Remove a login path from the login file.
print [command options] Print all the options for a specified
login path.
reset [command options] Deletes the contents of the login file.
help Display this usage/help information.
[root@student ~]# mysql_config_editor set --login_path=client --user=root --password --host=localhost --port=3306
Enter password:
会在当前用户HOME文件夹下创建 一个隐藏二进制文件
[root@student ~]# ls -al .mylogin.cnf
-rw------- 1 root root 156 Oct 12 21:37 .mylogin.cnf
[root@student ~]#
[root@student ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 18
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>
system variables
选项与变量
相同点:可以在命令行指定,在选项文件中指定
不同点:
选项:
任何可执行文件都有选项,客户端,mysqld
有些选项有值 ,有些选项没有值
在命令的一次执行范围内,选项不可改变量
变量:
用在服务器端,用来 对服务器进行设置
所有变量都有值,一开始都有默认值
变量可以动态修改
变量的作用范围
global:变量针对整个mysql生效
session :只针对当前会话
mysql> show variables; 列出所有的变量;
mysql> show variables like '变量名';
mysql> show variables like '%变量%';
mysql> select @@global.autocommit; 查看全局变量autocommit;
mysql>select @@session.autocommit; 查看会话级变量autocommit;
mysql> show global variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | ON |
+---------------+-------+
1 row in set (0.00 sec)
变量的设置:set 命令
set global 变量=变量值
set session 变量=变量值
变量的设置:
1、global变量 : 比如 max_connections
set global max_connections=1000;在一个会话中修改后,其它会话中可以看到结果 ;
set max_connections=1000;出错;
set session max_connections=1000; 出错;
2、有些变量既有golbal作用,又有session作用范围,比如autocommit,sql_mode,wait_timeout
set global autocommit=0 ;修改结果在新会话中生效,在现存的会话中不生效
select @@global.autocommit ,@@session.autocommit;
set session wait_timeout=30000;修改结果仅在当前会话中生效;
3、有些变量只有sessoin作用范围,比如
变量的持久性
通过set命令修改的变量仅对mysqld的本次启动有效;
解决方法
1、把变量放在选项文件中
2、执行命令set prersist 变量=变量值 ,修改结果被写入文件/var/lib/mysql/mysqld-auto.cnf或
datadir目录中,其格式为json;
仅针对global变量
mysql> show variables like 'max_connections';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 151 |
+-----------------+-------+
1 row in set (0.00 sec)
mysql> set global max_connections=1000;
mysql>
mysql> show variables like 'max_connections';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 1000 |
+-----------------+-------+
1 row in set (0.00 sec)
mysql>
mysql> show variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | ON |
+---------------+-------+
1 row in set (0.00 sec)
mysql> set global autocommit=0;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | ON |
+---------------+-------+
1 row in set (0.00 sec)
mysql> set global autocommit=0; 在新会话中生效;
mysql> exit;
[root@student ~]# mysql
mysql> show variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | OFF |
+---------------+-------+
1 row in set (0.00 sec)
mysql>
会话级变量的设置
mysql> set session wait_timeout=2009; 只有在当前会话才生效;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'wait_timeout';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wait_timeout | 2009 |
+---------------+-------+
1 row in set (0.00 sec)
mysql>
mysqladmin工具:用来管理mysqld服务,格式:
MySQLadmin 选项 命令
# mysqladmin --help;
[root@student ~]# mysqladmin -uroot -pOra_123 status
[root@student ~]# mysqladmin -uroot -pOra_123 ping
[root@student ~]# mysqladmin -uroot -pOra_123 shutdown
[root@student ~]# mysqladmin -uroot -pOra_123 version
mysql> select version();
[root@student ~]# mysqladmin -uroot -pOra_123 password
[root@student ~]# mysqladmin -uroot -pOra_123 create db_1
[root@student ~]# mysqladmin -uroot -pOra_123 drop db-1
mysqld进程:多线程
数据文件:被划分为大小相同的page,page大小通过变量innod
mysqld服务实现了三层功能:
1、connection层
启动connection线程
提供通信协议
对用户的登录进行验证
2、SQL层
分析、检查SQL的语法格式,把SQL转化为二进制代码
autthorization:检查当前用户对表是否有相太的权限
优化:针对SQL产生最佳的执行计划。
执行SQL。
产生日志,写入日志缓冲区。
3、storage层
实现数据的存储
存储引擎:
数据在存储介质中如何存储
当用户访问数据时,数据如何从介质读入内存
数据在内存中如何被访问
内存中被修改的数据如何被写的内存
通信协议:
TCP/IP: 不同操作系统,本地连接,远程连接
socket: UNIX/Linux本地连接
shared memory: windows系统,本地连接
named pipe :windows系统,本地连接
登录有关的选项:
-u 指定用户名,默认为root用户
-h 指定数据库服务器的IP地址,默认为localhost
-p 指定password
-P 指定服务器的PORT,默认为3306
# mysql -pOra_123
ip地址:
1、localhost:
本次使用socket通讯,可以进一步指定socket文件
mysql> status;
--------------
mysql Ver 8.0.13 for linux-glibc2.12 on x86_64 (MySQL Community Server - GPL)
Connection id: 16
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 8.0.13 MySQL Community Server - GPL
Protocol version: 10
Connection: Localhost via UNIX socket
mysql> show variables like 'socket';
+---------------+------------------------+
| Variable_name | Value |
+---------------+------------------------+
| socket | /data/mysql/mysql.sock |
+---------------+------------------------+
1 row in set (0.00 sec)
# mysql -pOra_123 -S /data/mysql/mysql.sock #指定用socket协议
#mysql -pOra_123 --protocol=tcp #指定用tcp协议
mysql> status
--------------
mysql Ver 8.0.13 for linux-glibc2.12 on x86_64 (MySQL Community Server - GPL)
...
Server version: 8.0.13 MySQL Community Server - GPL
Protocol version: 10
Connection: localhost via TCP/IP
...
2、127.0.0.1
本次登录使用tcp通信,可以进一步指定服务器的端口。
端口的查看:
mysql> show variables like 'port';
# mysql -uroot -pOra_123 -h127.0.0.1 -P 3306
mysql> status
--------------
...
Connection: 127.0.0.1 via TCP/IP
...
TCP port: 3306
...
3、有效的IP
在mysql中执行status命令验证连接情况
用户帐号:用户名称@ip地址:
# mysql -uroot -pOra_123 -h 192.168.11.222
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1130 (HY000): Host 'student' is not allowed to connect to this MySQL server
mysql> select user,host from user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)
rpm格式安装mysqld服务的启动/关闭:root用户
systemctl start mysqld
systemctl status mysqld
systemctl stop mysqld
如何用普通用户启用和关闭mysql
1、在操作系统中对mysql进行解决
cat /etc/passwd |grep mysql
systemctl stop mysqld
usermod -d /home/mysql -s /bin/bash mysql
mkdir /home/mysql
chown mysql:mysql /home/mysql
password mysql #输入密码
2、以mysql用户登录操作系统
su - mysql
启动mysqld
mysqld --defaults-file=/etc/my.cnf &
mysql用户在mysql数据库要创建
mysqladmin -umysql -pmysql ping
mysqladmin -umysql -pmysql shutdown
二进制安装
systemctl start mysql
systemctl status mysql
systemctl stop mysql
rpm格式mysql的弊端
1、处理安全考虑,生产系统不允许安装rpm格式
2、 mysql服务的启动,关闭需要用root用户
rpm格式启动、关闭:mysqld
root用户:
systemctlstartmysqld
systemctlstatusmysqld
systemctl stopmysqld
普通用户(如mysql用户):
mysqid --defaults-file=/etc/my.cnf&
mysqladmin -u -p.. shutdown
mysqladmin -u.. -p.. ping
mysqladmin-u.. -p.. status
绿色版:
编辑选项文件
mysqld--defaults-file=/mysql80/mysqld.cnf--initialize
mysqld--defaults-file=/mysql80/mysqld.cnf&
查看初始密码
mysqladmin-u..-p.shutdown
mysqladmin -u.. -p.. ping
mysqladmin-u..-p..status