一、环境准备
实验环境:centos 7
数据库版本:5.7.22
二、数据库安装
2.1 下载
[root@localhost ~]# cd /usr/local/src
[root@localhost src]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
2.2 解压
[root@localhost src]# tar -zxvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
2.3 复制
[root@localhost src]# cp -r mysql-5.7.22-linux-glibc2.12-x86_64 /usr/local/mysql
2.4 添加系统mysql组和mysql用户
[root@localhost src]# groupadd mysql
[root@localhost src]# useradd -r -g mysql mysql
2.5 安装数据库
切到mysql安装目录
[root@localhost src]# cd /usr/local/mysql
修改当前目录拥有者为mysql用户
[root@localhost mysql]# chown -R mysql:mysql ./
安装数据库
[root@localhost mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2018-08-13T03:20:19.548679Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-08-13T03:20:20.166625Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-08-13T03:20:20.333087Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-08-13T03:20:20.426986Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: cefdd8c5-9ea7-11e8-93d9-080027b2b4d5.
2018-08-13T03:20:20.429236Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-08-13T03:20:20.432428Z 1 [Note] A temporary password is generated for root@localhost: )dKi<;pH#2oy
上面生成了root账户的临时密码
创建RSA private key
[root@localhost mysql]# bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data
Generating a 2048 bit RSA private key
.......................................+++
...+++
writing new private key to 'ca-key.pem'
-----
Generating a 2048 bit RSA private key
....................................................................+++
...............................+++
writing new private key to 'server-key.pem'
-----
Generating a 2048 bit RSA private key
.............+++
.......................................+++
writing new private key to 'client-key.pem'
-----
2.6 修改my.cnf
创建binlog存放目录(此举是为了下面在my.cnf文件中配置error.log和binlog相关参数)
[root@localhost data]# cd /
[root@localhost /]# mkdir data
[root@localhost /]# cd data
[root@localhost data]# mkdir mysql
[root@localhost data]# cd mysql
[root@localhost mysql]# mkdir log
[root@localhost mysql]# cd log
[root@localhost log]# touch error.log
将/data/mysql/log整个目录及其子目录、子文件加入mysql用户组
[root@localhost mysql]# chown -R mysql:mysql /data/mysql/log
修改/etc/my.cnf文件
[root@localhost mysql]# vi /etc/my.cnf
配置如下:
[mysqld]
# basic settings #
user = mysql
port = 3306
sql_mode = "STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER"
autocommit = 1
server-id = 1
character_set_server = utf8mb4
transaction_isolation = READ-COMMITTED
lower_case_table_names = 1
explicit_defaults_for_timestamp = 1
max_allowed_packet = 16777216
event_scheduler = 1
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
socket = /tmp/mysql.sock
pid-file = /usr/local/mysql/data/mysqld.pid
# connection #
interactive_timeout = 1800
wait_timeout = 1800
lock_wait_timeout = 1800
skip_name_resolve = 1
max_connections = 1000
max_connect_errors = 1000000
# table cache performance settings #
table_open_cache = 4096
table_definition_cache = 4096
table_open_cache_instances = 128
# log settings #
log_error = /data/mysql/log/error.log
slow_query_log_file = /data/mysql/log/slow.log
log-bin = /data/mysql/log/mysql-bin
relay_log = mysql-relay-bin
general_log_file = general.log
slow_query_log = 1
log_queries_not_using_indexes = 1
log_slow_admin_statements = 1
log_slow_slave_statements = 1
log_throttle_queries_not_using_indexes = 10
long_query_time = 1
min_examined_row_limit = 100
binlog-rows-query-log-events = 1
log-bin-trust-function-creators = 1
expire-logs-days = 7
log-slave-updates = 1
# replication settings #
master_info_repository = TABLE
relay_log_info_repository = TABLE
sync_binlog = 1
gtid_mode = on
enforce_gtid_consistency = 1
log_slave_updates
binlog_format = ROW
binlog_rows_query_log_events = 1
relay_log = relay.log
relay_log_recovery = 1
slave_skip_errors = ddl_exist_errors
slave-rows-search-algorithms = 'INDEX_SCAN,HASH_SCAN'
replicate-ignore-db = mysql
replicate-ignore-db = information_schema
replicate-ignore-db = performance_schema
replicate-ignore-db = sys
[mysqld-5.7]
# new innodb settings #
loose_innodb_numa_interleave = 1
innodb_buffer_pool_dump_pct = 40
innodb_page_cleaners = 16
innodb_undo_log_truncate = 1
innodb_max_undo_log_size = 2G
innodb_purge_rseg_truncate_frequency = 128
# new replication settings #
slave-parallel-type = LOGICAL_CLOCK
slave-parallel-workers = 16
slave_preserve_commit_order = 1
slave_transaction_retries = 128
# other change settings #
binlog_gtid_simple_recovery = 1
log_timestamps = system
show_compatibility_56 = on
2.7 设置mysql自动启动
将服务文件拷贝到init.d下,并重命名为mysqld
[root@localhost data]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
修改/etc/init.d/mysqld文件
[root@localhost data]# vi /etc/init.d/mysqld
设置basedir和datadir的路径,配置如下:
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
赋予可执行权限
[root@localhost data]# chmod +x /etc/init.d/mysqld
加入开机启动
[root@localhost data]# chkconfig --add mysqld
显示服务列表
[root@localhost data]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
2.8 启动mysql服务并初始化密码
启动mysql服务
[root@localhost log]# service mysqld start
Starting MySQL.. SUCCESS!
设置软链接
[root@localhost bin]# ln -s /usr/local/mysql/bin/mysql /usr/bin
初始化root密码
[root@localhost bin]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or /g.
Your MySQL connection id is 4
Server version: 5.7.22-log
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> alter user 'root'@'localhost' identified by 'root';
Query OK, 0 rows affected (0.06 sec)
mysql> grant all privileges on *.* to'root'@'%' identified by 'root';
Query OK, 0 rows affected, 1 warning (0.04 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
重新登录
mysql> exit
Bye
[root@localhost bin]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or /g.
Your MySQL connection id is 5
Server version: 5.7.22-log 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
至此,mysql数据库安装完成。
版权声明:本文为weixin_39845780原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。