接上一节,本次系统环境为centos7.4 x64
1.1. mysql5.7的安装
安装方式和mysql5.6一样,区别就是废弃了mysql_install_db的初始化命令。
1.1.1. 创建mysql用户并指定mysql用户组。
创建用户并指定用户组
groupadd mysql
useradd -g mysql mysql -s /sblin/nologin
centos7.4 已默认创建mysql用户
1.1.2. 解压安装包
软件包家目录统一放在/usr/local
# cd / usr/local
解压mysql软件包
[root@localhost local]# tar -zxvf /orasoft/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz注意:书中示例版本为mysql5.7.14,本次实验环境为mysql5.7.24
重命名解压目录为mysql;
[root@localhost local]# mv mysql-5.7.24-linux-glibc2.12-x86_64 mysql
给mysql目录授权
chown mysql:mysql -R mysql
1.1.3. 创建数据库目录
创建mysql数据库的数据目录datadir,创建在/data/mysql下面;
mkdir -p /data/mysql
chown mysql:mysql -R /data/mysql
1.1.4. 配置文件
由于是二进制安装方式 ,数据库配置文件需要自己配置好。配置文件如下:
[root@localhost mysql]# cat /etc/my.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set=utf8
[mysql]
prompt="\u@db \R:m:\s [\d]>"
#no-auto-rehash
auto-rehash
[mysqld]
port=3306
basedir=/usr/local/mysql
socket=/tmp/mysql.sock
datadir=/data/mysql
character-set-server=utf8mb4
skip_name_resolve=1
open_files_limit=65535
back_log=1024
max_connections=512
max_connect_errors=100000
table_open_cache=1024
table_definition_cache=1024
table_open_cache_instances=64
thread_stack=512K
external-locking=FALSE
max_allowed_packet=128M
sort_buffer_size=4M
join_buffer_size=4M
thread_cache_size=768
query_cache_size=0
query_cache_type=0
interactive_timeout=600
wait_timeout=600
tmp_table_size=32M
max_heap_table_size=32M
slow_query_log=1
slow_query_log_file=/data/mysql/slow.log
log-error=/data/mysql/error.log
long_query_time=0.5
server-id=3306100
log-bin=/data/mysql/mysql-binlog
sync_binlog=1
binlog_cache_size=4M
max_binlog_cache_size=1G
max_binlog_size=1024M
expire_logs_days=7
master_info_repository=TABLE
relay_log_info_repository=TABLE
gtid_mode=on
enforce_gtid_consistency=1
log_slave_updates=1
binlog_format=row
relay_log_recovery=1
relay-log-purge=1
key_buffer_size=32M
read_buffer_size=8M
read_rnd_buffer_size=4M
bulk_insert_buffer_size=64M
lock_wait_timeout=3600
explicit_defaults_for_timestamp=1
default-storage-engine=InnoDB
interactive_timeout=300
wait_timeout=300
transaction_isolation=REPEATABLE-READ
innodb_thread_concurrency=0
innodb_sync_spin_loops=100
innodb_spin_wait_delay=30
innodb_buffer_pool_size=1434M
innodb_buffer_pool_instances=8
innodb_buffer_pool_load_at_startup=1
innodb_buffer_pool_dump_at_shutdown=1
innodb_data_file_path=ibdata1:1024M:autoextend
innodb_flush_log_at_trx_commit=1
innodb_log_buffer_size=32m
innodb_log_file_size=2G
innodb_log_files_in_group=2
innodb_io_capacity=2000
innodb_io_capacity_max=4000
innodb_flush_neighbors=0
innodb_write_io_threads=8
innodb_read_io_threads=8
innodb_purge_threads=4
innodb_page_cleaners=4
innodb_open_files=65535
innodb_max_dirty_pages_pct=50
innodb_flush_method=O_DIRECT
innodb_lru_scan_depth=4000
innodb_checksum_algorithm=crc32
innodb_lock_wait_timeout=10
innodb_rollback_on_timeout=1
innodb_print_all_deadlocks=1
innodb_file_per_table=1
innodb_online_alter_log_max_size=4G
internal_tmp_disk_storage_engine=InnoDB
innodb_stats_on_metadata=0
innodb_status_file=1
innodb_status_output=0
innodb_status_output_locks=0
performance_schema=1
performance_schema_instrument='%=on'
innodb_monitor_enable="module_innodb"
innodb_monitor_enable="module_server"
innodb_monitor_enable="module_dml"
innodb_monitor_enable="module_ddl"
innodb_monitor_enable="module_trx"
innodb_monitor_enable="module_os"
innodb_monitor_enable="module_purge"
innodb_monitor_enable="module_log"
innodb_monitor_enable="module_lock"
innodb_monitor_enable="module_buffer"
innodb_monitor_enable="module_index"
innodb_monitor_enable="module_ibuf_system"
innodb_monitor_enable="module_buffer_page"
innodb_monitor_enable="module_adaptive_hash"
# 后增加
pid-file=/data/mysql/mysql.pid
[mysqldump]
quick
max_allowed_packet=32M
备注
命令自动补全功能
关闭:no-auto-rehash
开启:auto-rehash
1.1.5. 初始化数据库
[root@localhost ~]# cd /usr/local/mysql/bin
# ./mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --user=mysql --initialize
不需要参数:--defaults-file,默认参数文件就是/etc/my.cnf
初始化过程中加-initialize参数,生成随机密码写入log-error中;
如加上--initialize-insecure参数,则无密码写入。
在log-err日志中,记录生成的随机密码
[root@localhost mysql]# cat error.log |grep password
2018-11-05T01:53:36.883537Z 1 [Note] A temporary password is generated for root@localhost: Z/22rKZWIGvh
1.1.6. 添加mysql启动脚本到系统服务
添加到MySQL 启动脚本到系统服务
# cp support-files/mysql.server /etc/init.d/mysqld
# chmod 755 /etc/init.d/mysqld
(*注意,mysql二进制安装默认的路径为/usr/local/mysql,启动脚本里/usr/local/mysql需要替换,如安装位置变换为/data/mysql56
>sed -i 's#/usr/local/mysql#/data/mysql56#g' /data/mysql56/bin/mysqld_safe /etc/init.d/mysqld )
配置环境变量
# echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile
# source /etc/profile
添加到系统服务,可用系统启动
# service mysqld start
添加自动启动
> chkconfig --add mysqld
> chkconfig mysqld on
> chkconfig --list mysqld
1.1.7. 启动MySQL服务
启动脚本有两个分别是:
/usr/local/mysql/bin/mysqld_safe &
/usr/local/mysql/support-files/mysql.server(即/etc/init.d/mysqld)
当启动mysqld时,mysqld_safe同时启动
mysqld_safe监控mysqld服务,记录错误日志,并在mysqld因故障停止时将其重启
启动方式一
$ bin/mysqld_safe --user=mysql &
启动方式二
$ service mysql.server start
或者
/usr/local/mysql/support-files/mysql.server start
1.1.8. 设置MySQL密码
用初始密码登陆,修改root密码并修改为永不过期。
[root@localhost etc]# mysql -uroot -pZ/22rKZWIGvh
mysql> alter user 'root'@'localhost' identified by 'root123';
或
mysql> SET PASSWORD ='root123';
mysql> alter user 'root'@'localhost' password expire never;
mysql>flush privileges;
mysql> exit;
查看mysql.user表中存在哪些帐户 以及它们的密码是否为空:
MySQL 5.7.6起,使用这个语句:
mysql> SELECT User, Host, HEX(authentication_string) FROM mysql.user;
+---------------+-----------+-------------------------------------------+
| User | Host | HEX(authentication_string) |
+---------------+-----------+------------------------------------------------------------------------------------+
| root | localhost | 2A39383730334637413534333934344644333831383037373636394637344436303631364442324338 |
| mysql.session | localhost | 2A5448495349534E4F544156414C494450415353574F52445448415443414E42455553454448455245 |
| mysql.sys | localhost | 2A5448495349534E4F544156414C494450415353574F52445448415443414E42455553454448455245 |
+---------------+-----------+------------------------------------------------------------------------------------+
1.1.9. 开启远程登录
以权限用户root登录
# mysql -uroot -proot123mysql> use mysql;
mysql> use mysql;
mysql> update user set host = '%' where user ='root';
或者
mysql> grant all privileges on *.* to 'root'@'%' identified by 'mima' with grant option;
mysql> flush privileges;
[mysql]>SELECT User, Host, HEX(authentication_string) FROM mysql.user;
+---------------+-----------+------------------------------------------------------------------------------------+
| User | Host | HEX(authentication_string) |
+---------------+-----------+------------------------------------------------------------------------------------+
| root | % | 2A46414146464536343445393031434641464145433735363234313545354641454332343342384232 |
| mysql.session | localhost | 2A5448495349534E4F544156414C494450415353574F52445448415443414E42455553454448455245 |
| mysql.sys | localhost | 2A5448495349534E4F544156414C494450415353574F52445448415443414E42455553454448455245 |
+---------------+-----------+------------------------------------------------------------------------------------+
1.1.10. 生成RSA私钥
可以跳过此步骤,可选,以后研究。
mysql_ssl_rsa_setup需要openssl支持,用于启用数据量ssl连接,需要进一步配置。
参考-MySQL 使用 SSL 连接:https://segmentfault.com/a/1190000007819751
$ bin/mysql_ssl_rsa_setup
