review

Dear friends, I am glad to meet you again, and, after learning of the previous chapter, we simply know a bit Mysql, Mysql learned of the development process, the company branch Mysql, Mysql's advantage and simple dissected part of Mysql .

According to the chapter installation is complete after the required environment (if beginner Linux students have any questions can send questions to my mailbox, contact assistant micro letter on the installation of the operating system: cto51boke acquisition), this chapter begins in our be environmentally actual construction, the building refer to the following several versions, and which describes the difference between where the 5.6 and 5.7 should be required before friends, here the 8.0 installation is also installed together with demonstration

Mysql5.6.34(Mysql稳定版本)
Mysql5.7.20(Mysql稳定版本) Mysql8.0.15(官方最新GA版本)

Installation Introduction

? Before we said very lightweight Mysql, Mysql then installed it must be very simple answer: Yes, very simple, but the installation of rigid everyone on the Internet to find articles and anybody can build out of nothing more than the length of time Bale, so after taking into account everyone in the production environment is installed, we will need to take into account the direct preparation to do the whole, before installing Mysql we need to optimize your environment then after installation, such as a birth of the baby, he can be born in a warm and family growth, charges did not talk much to say, look at my basic environment (three versions I was three virtual machine installation, limited conditions of friends can try to repeat the installation)

机器1:
网络:桥接
OS:CentOS7.4
CPU:4核
内存:8G 磁盘:100G 机器2: 网络:桥接 OS:CentOS7.4 CPU:4核 内存:8G 磁盘:100G 机器3: 网络:桥接 OS:CentOS7.4 CPU:4核 内存:8G 磁盘:100G

Preparing the Environment

Environmental ready 5.6,5.6,8.0 are the same

1. Network: because we are currently learning to use, so it can be a server firewall, selinux be closed, to avoid affecting when we learn some communication problems, file permissions and so on written questions, the general production on selinux is not open, firewall is performed on hardware maintenance, so we can be close, closed as follows:

CentOS7:
防火墙临时关闭:systemctl stop firewalld
关闭防火墙开机自启:systemctl disable firewalld
selinux临时关闭:setenforce 0
selinux永久关闭:sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/sysconfig/selinux

CentOS6:
防火墙临时关闭:service iptables stop 关闭防火墙开机自启:chkconfig iptables off selinux临时关闭:setenforce 0 selinux永久关闭:sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/sysconfig/selinux

We suggest that you will perform all four parts of the operation, or service may still be in the open state, such as selinux modified the configuration file does not take effect immediately

Selinux simple with this: the role of the firewall we all know, is the protection of the external service, Selinux popular to say it is to protect the main Linux file write, modify, delete protection, the specific set of rules can be configured, interested friends can look at to understand, with very little production right now, so do not focus as

2.CPU IO scheduling model: IO scheduling mode is also crucial to the database, Mysql is IO scheduling by the CPU to get data disk, but the IO scheduler there are different scenarios of adaptation, such as different options SAS disks and solid state disks, let us look together,

查看当前IO调度模式:[]内为默认
[root@localhost /data/server]# cat /sys/block/sda/queue/scheduler
noop [deadline] cfq 
[root@localhost /data/server]# 

1)cfg模式为比较通用的算法,保证所有任务尽量公平
此模式会给进程任务分配一个请求队列与时间片
在此时间片内由进程向设备文件进行请求,时间片消耗完后被挂起等待调度
2)deadline模式是在cfg的基础上,确保了在一个截止时间内服务请求,这个截止时间是可调整的 而默认读期限短于写期限.这样就防止了写操作因为不能被读取而饿死的现象 deadline是数据库服务器IO模型的最好选择 优先处于读请求 比较适合于SAS似机械磁盘 3)noop模式只有一个队列,每当有新的请求进来会将其与最近请求进行合并设备文件 此模式优先写而饿死读操作,与deadline相点相反的意思 此模式如果是机械磁盘极度不建议使用,如果是固态磁盘则可以尝试 修改当前IO调度模式: Centos7:grubby --update-kernel=ALL --args="elevator=deadline" Centos6:vi /etc/grub.conf 尾行添加 elevator=deadline echo deadline > /sys/block/sda/queue/scheduler 

3.SWAP use intensity: SWAP is a protective mechanism of the operating system memory, it is on the disk to memory-mapped files, low memory or memory tasks inactive operating system will call up to SWAP these tasks in order to prevent the system load is too high, causing the operating system directly to hang, the operating system is your brain, the brain burned, her body parts were all futile, but excessive use of SWAP will reduce the performance of the database, because it is not in itself memory, so we take a tradeoff value, while ensuring there SWAP use, but try not to use (if your company is not bad money, the performance of the machine are particularly high, it can directly swap is set to 0, the main library if down machine directly switching from the library oriented performance)

查看当前分配比例:
[root@localhost /data/server]# cat /proc/sys/vm/swappiness
30
[root@localhost /data/server]# 

修改使用优先级比例:
当前修改:echo 10 > /proc/sys/vm/swappiness 开机调用:echo -e "echo 10 > /proc/sys/vm/swappiness" >> /etc/rc.d/rc.local

4. File System: the default file system CentOS6 as the default ex4,7 as XFS, conduct business database selection we recommend using XFS, subsequent indexing will get to learn Mysql Mysql was B + TREE structure, but also that XFS kinds of storage, combined with Mysql better, and the file system type is more stable

CentOS6格式化XFS需要操作如下:
1.yum install -y xfsprogs xfsdump 2.将文件系统格式化为xfs格式 

5. System parameters: Mysql itself provide services to applications, so there will be a lot of connection to the production Mysql, and file data concurrently read, while Linux itself in order to prevent too many concurrent processes cause a system crash of the parameters has been restricted, we can make changes directly

查看:ulimit -a
1.打开文件的句柄数:open files(防止too many open files错误) 
2.针对用户数量限制:max user processes (防止单机多实例,连接数过多拒绝新连接)

修改:
echo -e "* soft nproc 65535\n* hard nproc 65535\n* soft nofile 65535\n* hard nofile 65535\n" >> /etc/security/limits.conf 生效:退出当前终端重新登陆ulimit –a查看两项参数是否已变更为65535 

Above we completed the basic environment ready, following on with a start Mysql installation


Mysql5.6 installation

一、环境准备:
1.目录创建:mkdir -p /data/{software,mysql}
           mkdir -p /data/mysql/{data,log,tmp}

2.用户创建:useradd mysql

3.安装依赖包:yum install perl perl-devel perl-Data-Dumper libaio-devel -y

二、安装:
1.下载包文件:cd /data/software/
         wget https://cdn.mysql.com/archives/mysql-5.6/mysql-5.6.34-linux-glibc2.5-x86_64.tar.gz
         tar -zxvf mysql-5.6.34-linux-glibc2.5-x86_64.tar.gz
         mv mysql-5.6.34-linux-glibc2.5-x86_64 /usr/local/mysql

2.权限赋值:chown -R mysql:mysql /usr/local/mysql /data/mysql/*

三、配置文件: vi /etc/my.cnf
大家也可以在群里找我要文件发送给大家(后续我们一一讲解参数的意义)

[client]
port    = 3306
socket  = /data/mysql/tmp/mysql.sock

[mysql]
prompt="\u@db \R:\m:\s [\d]> "
no-auto-rehash

[mysqld]

user    = mysql
port    = 3306
basedir = /usr/local/mysql
datadir = /data/mysql/data
socket  = /data/mysql/tmp/mysql.sock
pid-file = /data/mysql/tmp/mysql.pid
character-set-server=utf8
collation-server = utf8_general_ci
#skip-character-set-client-handshake=true
#init_connect='insert into auditlog.accesslog(ConnectionID,ConnUserName,PrivMatchName,LoginTime) values(connection_id(),user(),current_user(),now());'
skip_name_resolve=1
event_scheduler = on
sql_mode='NO_UNSIGNED_SUBTRACTION,NO_ENGINE_SUBSTITUTION'

open_files_limit = 65535
innodb_open_files = 65535 
back_log=1024
max_connections = 512 
max_connect_errors=1000000 
interactive_timeout=300 
wait_timeout=300 
max_allowed_packet = 1024M

table_open_cache=2048 
table_definition_cache=2048 
table_open_cache_instances = 32
thread_cache_size = 128 
thread_stack = 512K 
external-locking = FALSE 

max_tmp_tables=200 
tmp_table_size=100M 
max_heap_table_size=100G 
explicit_defaults_for_timestamp = 1
lock_wait_timeout = 3600 
auto_increment_increment = 1 
auto_increment_offset = 1 
autocommit = ON 
secure_file_priv='' 
read_only = OFF 
lower_case_table_names=1 
innodb_fast_shutdown = 0
innodb_force_recovery=0
innodb_buffer_pool_dump_at_shutdown = 1 
innodb_buffer_pool_load_at_startup = 1 

log-error=/data/mysql/log/error.log 

slow_query_log=ON 
slow_query_log_file=/data/mysql/log/slow_mysql.log 
long_query_time=2

innodb_flush_log_at_trx_commit=1 
innodb_log_file_size =1G 
innodb_log_files_in_group=3 
innodb_log_group_home_dir=./ 

sync_binlog = 1  
binlog_cache_size = 16M 
max_binlog_cache_size = 1G 
max_binlog_size=1G 
expire_logs_days = 30

default-storage-engine = INNODB 
#internal_tmp_disk_storage_engine = INNODB
transaction_isolation=REPEATABLE-READ
innodb_max_dirty_pages_pct = 50 
innodb_adaptive_flushing = ON 
innodb_flush_method = O_DIRECT 

sort_buffer_size=8M 
join_buffer_size=8M 
query_cache_size=0 
query_cache_type=0 
read_buffer_size = 8M
optimizer_switch="index_condition_pushdown=on,mrr=on,mrr_cost_based=on,batched_key_access=off,block_nested_loop=on" read_rnd_buffer_size = 8M innodb_old_blocks_pct=35 #innodb_additional_mem_pool_size= 128M innodb_buffer_pool_size= 1G innodb_buffer_pool_instances = 16 innodb_log_buffer_size =32M bulk_insert_buffer_size=128M innodb_change_buffer_max_size = 50 innodb_doublewrite=on innodb_adaptive_hash_index = on innodb_file_per_table =1 innodb_data_file_path = ibdata1:1024M:autoextend innodb_page_size = 16k #innodb_checksum_algorithm =crc32 innodb_lock_wait_timeout = 35 innodb_rollback_on_timeout = on innodb_sync_spin_loops = 100 innodb_spin_wait_delay = 30 innodb_lru_scan_depth = 4000 innodb_thread_concurrency = 0 innodb_write_io_threads = 2 innodb_read_io_threads = 2 innodb_purge_threads = 2 innodb_io_capacity = 800 innodb_io_capacity_max = 1600 server-id = 3306110 log-bin= /data/mysql/log/binlog-mysql binlog_format=row 四、初始化Mysql 1.查看初始化使用帮助:/usr/local/mysql/scripts/mysql_install_db --help 2.初始化当前Mysql:/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql/data --defaults-file=/etc/my.cnf --user=mysql 3.初始化完成后会提示你修改密码:/usr/local/mysql/bin/mysqladmin -u root password '123456' 4.查看初始化后数据库文件:ls /data/mysql/data/ 如果包括mysql、information_schema、test目录即可以视为初始化成功 [root@localhost data]# ls ib_buffer_pool ibdata1 ib_logfile0 ib_logfile1 ib_logfile2 mysql test undo001 undo002 undo003 5.最后我们启动mysql:/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &

We look at Pictured: OK prompt will appear after two normal initialization, and prompt us to use the mysqladmin password command for the initial configuration, use Mysql last start mysqld_safe way, when we started to manually specify the location of the configuration file, in fact, also you can not specify, but you need to know when Mysql order to find the configuration file, as follows:

/etc/my.cnf 
/etc/mysql/my.cnf 
/usr/local/mysql/etc/my.cnf


Mysql5.7 installation

Basically the same Mysql5.7 installation and installation of 5.6, difference is that the initialization section, we look at different places

5.7初始化Mysql:
初始化操作:/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql/data --user=mysql --initialize

查看随机密码:可以看到我们的初始密码为 "Eiv*/Dy!I44t"
[root@localhost data]# cat /data/mysql/log/error.log  | grep password
2019-02-21T10:45:24.067928Z 1 [Note] A temporary password is generated for root@localhost: Eiv*/Dy!I44t 5.启动mysql:/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &

The difference between 5.6 and 5.7 summarize: here describes only different after initialization, we explain the different follow-up on more features

Mysql5.6:
1.初始化命令为mysql_install_db,初始化之后会由我们自己来进行密码的初次修改
2.数据目录下包含mysql information_schema test performache_schema四个数据库

Mysq.5.7:
1.初始化命令为mysqld,并多了一个initialize参数
初始化之后默认会生成一个随机密码至error.log文件中
后续我们使用这个密码进行二次修改
这个密码只是Mysql提供给我们初次使用的
登陆之后会提示我们密码过期问题
所以我们一般在初始化之后直接对密码进行二次修改使用如下命令:

/usr/local/mysql/bin/mysqladmin -uroot -p password
输入刚刚的随机密码,再输入两次你需要的密码即可

2.数据目录下包含mysql information_schema sys performache_schema四个数据库
5.7初始化参数介绍:

initialize参数:当我们添加这个参数时Mysql会默认帮我们生成一个随机密码到error.log文件内 initialize-insecure:如果我们在5.7初始化时指定这个参数,那么此时会和5.6初始化后结果相同,不会为我们生成随机密码由用户进行第一次的密码配置 

Mysql8.0 installation

一、环境准备:
1.目录创建:mkdir -p /data/{software,mysql}
                     mkdir -p /data/mysql/{data,log,tmp}

2.用户创建:useradd mysql

3.安装依赖包:yum install perl perl-devel perl-Data-Dumper libaio-devel -y 二、安装: 1.下载包文件:cd /data/software/ wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.15-linux-glibc2.12-x86_64.tar.xz xz -d mysql-8.0.15-linux-glibc2.12-x86_64.tar.xz tar -xvf mysql-8.0.15-linux-glibc2.12-x86_64.tar mv mysql-8.0.15-linux-glibc2.12-x86_64 /usr/local/mysql 2.权限赋值:chown -R mysql:mysql /usr/local/mysql /data/mysql/* 三、配置文件: vi /etc/my.cnf 大家可以继续使用上面的配置文件内容 但需要将部分参数进行删减,因为有部分特性至Mysql8.0已经彻底废弃了,如下: max_tmp_tables=200 query_cache_size=0 query_cache_type=0 后续讲解8.0新特性时我们会添加一些新的配置参数 四、初始化 /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql/data --user=mysql --initialize 查看初始化后数据库文件:ls /data/mysql/data/ 包括 [root@localhost data]# ls auto.cnf ca.pem client-key.pem ibdata1 ib_logfile1 ibtmp1 mysql performance_schema public_key.pem server-key.pem undo_001 ca-key.pem client-cert.pem ib_buffer_pool ib_logfile0 ib_logfile2 #innodb_temp mysql.ibd private_key.pem server-cert.pem sys undo_002 [root@localhost data]# 五、最后我们启动mysql:/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf & 六、登入Mysql: [root@localhost log]# /usr/local/mysql/bin/mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 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. root@db 10:36: [(none)]> 

Mysql password reset

At this point, Mysql installation is completed we learn, that if we are not careful to file error.log not lost password input when it can not see the information, make changes or secondary password problems led password is not on after the installation is complete , how should I do that, then we can also use the following approach to password reset, the operation is very simple, the idea is to Mysql password permission to skip the table, you can not directly can get into the house keys inside the house, and it can do whatever they want, then we can go home to find our keys, or keys and the locks changed one, let's look at the actual operation

Note: Production is possible that we will not do this, the password will be strictly kept by the DBA or DBA manager, basically no loss occurs, and general business operations of the library is prohibited to stop, but everything can not guarantee 100% if maintenance when the password is lost resulting in inadequate specifications, can be selectively password reset


1)关闭Mysql:killall mysqld   或  ps aux | grep mysql | grep -v grep |awk '{print $2}' |xargs kill -9 (不建议使用此类方式停止Mysql,但目前为非常情况因为没有Mysql密码,所以不能使用mysqladmin shutdown的正常关库方式关闭Mysql) 2)使用跳过密码权限表的方式启动Mysql: /usr/local/mysql/bin/mysqld_safe –defaults-file=/etc/my.cnf –skip-grant-tables & 参数:--skip-grant-tables是跳过了mysql.user表的权限验证信息,让我们可以直接进入Mysql进行数据库操作 3)登陆:mysql (敲mysql命令就直接可以登入了) 4)查看用户:select user,host from mysql.user;(查看一下我们需要修改的用户信息) 5)修改密码:(Mysql的权限我们后续会再次进行讲解,先了解一下是由用户%主机+密码方式验证的) update mysql.user set authentication_string=PASSWORD('new-password') where user='root' and host='localhost'; 注意:此处我们使用的版本是5.7,以前版本使用如下命令,5.7之后密码的存储字段发生了变化: update mysql.user set password=PASSWORD('new-password') where user='root' and host='localhost'; 6)刷新权限:flush privileges;(Mysql修改user表后不会立即生效,需要执行此命令进行权限的刷新) 7)重启Mysql: 1.关闭Mysql:killall mysqld 或 ps aux | grep mysql | grep -v grep |awk '{print $2}' |xargs kill -9 2.启动Mysql:/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf & 

summary

In this chapter we learned Mysql5.6, Mysql5.7, Mysql8.0 each version of the installation process, and we should introduce the operating system to optimize the knowledge before you install Mysql, if while at work or exercise the forgotten root password should be how to process, suggest that you practice must be repeated practice, and in the early learning is the best way to practice hand to knock, do not copy and paste, so although the efficiency is high, but we are now learning stage, Do not go fast, knock a few times so that it remembers firm, including back some of the knowledge problem encountered in the study may have a roadmap, Mysql installation is very simple, it is divided into five parts altogether, many applications are basically service can be summarized in five sections: environment preparation, installation, configuration, initialization, start, go check if you experience problems during installation whether their action is in accordance with the operation to the teacher, and then go to check their the environment is not a problem, and more brains to think about the next chapter we began to learn Mysql.user this table, And some other aspects of the authority for the control, access control in a production environment is inevitable, if the control rights of users and developers is not good, that is a problem is the question of your DBA, we will also introduce some 8.0 into a new feature, we will again next chapter!