1. 二进制安装mysql-5.7.39
1.1 下载mysql
访问:https://downloads.mysql.com/archives/community/
https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.39-linux-glibc2.12-x86_64.tar.xz
1.2 卸载系统自带数据库
rpm -aq | grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
1.3 安装配置mysql
安装mysql依赖包
yum install -y libaio libaio-devel
创建用户和组
useradd mysql -s /sbin/nologin
解压数据mysql
cd /usr/local/src
tar -xvf mysql-5.7.39-linux-glibc2.12-x86_64.tar.xz -C /usr/local/
mv ../mysql-5.7.39-linux-glibc2.12-x86_64 ../mysql-5.7.39
或者
ln -s /usr/local/mysql-5.7.39-linux-glibc2.12-x86_64 /usr/local/mysql-5.7.39
创建数据库文件目录这里的data目录可以创建在存储或者其他任何地方合理即可
mkdir -p /data/mydata/{data,tmp,log}
chown -R mysql:mysql /data/mydata/
初始化数据库
创建my.cnf配置文件
vim /etc/my.cnf
[client]
port = 3306
socket = /data/mydata/tmp/mysql.sock
default-character-set = utf8mb4
[mysqld]
server_id=10
port = 3306
user = mysql
character-set-server = utf8mb4
collation-server=utf8mb4_bin
default_storage_engine = innodb
socket = /data/mydata/tmp/mysql.sock
basedir = /usr/local/mysql-5.7.39
datadir = /data/mydata/data
pid-file = /data/mydata/data/mysql.pid
transaction_isolation = READ-COMMITTED
lower_case_table_names = 1
max_connections = 5000
max_connect_errors = 10000
table_open_cache = 2048
table_definition_cache = 2048
table_open_cache_instances = 32
max_allowed_packet = 256M
read_buffer_size = 8M
read_rnd_buffer_size = 16M
skip_name_resolve = 1
open_files_limit = 65535
back_log = 1024
log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 30
master_info_repository = table
relay_log_info_repository = table
sync_binlog = 1
query_cache_size = 128M
query_cache_limit = 4M
sort_buffer_size = 16M
join_buffer_size = 16M
wait_timeout=3600
interactive_timeout=3600
group_concat_max_len=102400
#####====================================[innodb]==============================
#1)线程设置
innodb_thread_concurrency = 0
innodb_sync_spin_loops = 100
innodb_spin_wait_delay = 30
#2)缓存设置
innodb_buffer_pool_size = 1024M
innodb_buffer_pool_instances = 4
innodb_buffer_pool_load_at_startup = ON
innodb_buffer_pool_dump_at_shutdown = ON
#3)表空间设置
innodb_data_file_path = ibdata1:512M:autoextend
innodb_undo_logs = 128
innodb_max_undo_log_size = 2G
innodb_undo_tablespaces = 8
innodb_undo_log_truncate = 1
innodb_undo_directory = undolog
#4)redo log设置
innodb_log_buffer_size = 32M
innodb_log_file_size = 2G
innodb_log_files_in_group = 3
innodb_flush_log_at_trx_commit = 1
#5)IO设置
innodb_io_capacity = 8000
innodb_io_capacity_max = 15000
innodb_flush_neighbors = 0
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_purge_threads = 1
innodb_page_cleaners = 1
innodb_open_files = 65535
innodb_file_per_table = 1
innodb_max_dirty_pages_pct = 85
innodb_flush_method = O_DIRECT
innodb_lru_scan_depth = 4000
innodb_checksum_algorithm = crc32
innodb_lock_wait_timeout = 10
innodb_rollback_on_timeout = ON
innodb_print_all_deadlocks = ON
innodb_online_alter_log_max_size = 4G
innodb_stats_on_metadata = 0
innodb_checksums = ON
#6)performance_schema
performance_schema = ON
performance_schema_instrument = '%=on'
#7)innodb monitor
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"
#####====================================[log]==============================
log_timestamps = SYSTEM
log_error = /usr/local/mysql8028/logs/mysql-error.log
slow_query_log = 1
long_query_time = 3
log-queries-not-using-indexes = 1
log_throttle_queries_not_using_indexes = 60
min_examined_row_limit = 100
log_bin_trust_function_creators = 1
slow_query_log_file = /usr/local/mysql8028/logs/mysql-slow.log
sql_mode="STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION"
初始化mysql命令
/usr/local/mysql-5.7.39/bin/mysqld --defaults-file=/etc/my.cnf --initialize --basedir= /usr/local/mysql-5.7.39 --datadir=/data/mydata/data --user=mysql
3.4 配置开机启动
前台启动mysql
/usr/local/mysql-5.7.39/bin/mysqld_safe --defaults-file=/etc/my.cnf
或
/usr/local/mysql-5.7.39/support-files/mysql.server start
# 这里要注意如果更改了安装目录需要求改mysql.server中的basedir和datadir变量配置
加入init.d开机启动项启动
cp /usr/local/mysql-5.7.39/support-files/mysql.server /etc/init.d/mysqld
chkconfig mysqld on --level 35
service mysqld start|stop|status
systemctl enable mysqld
systemctl start|stop|status mysqld
3.5 登录mysql
前台启动mysql
echo 'export PATH=$PATH:/usr/local/mysql-5.7.39/bin' >>/etc/profile.d/mysql
source /etc/profile.d/mysql
登录mysql
mysql -u root -p #在初始化日志里找随机密码,然后更改密码;
mysql> alter user root@'localhost' identified by '123456';
mysql> flush privileges;
mysql> select user,host from mysql.user;
2. 报错
2.1 首次登录必须更改密码
mysql> use mysql;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
2.2 配置密码不符合密码复杂度报错
mysql> alter user 'root'@'localhost' identified by '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
validate_password_policy 默认值是1,即MEDIUM,所以设置的密码必须符合长度,且必须含有数字,小写或大写字母,特殊字符;
validate_password_length 控制密码长度参数 默认值为8;
mysql> install plugin validate_password soname 'validate_password.so'; #安装启用validate_password
mysql> show variables like 'validate_password%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password_check_user_name | OFF |
| validate_password_dictionary_file | |
| validate_password_length | 8 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | MEDIUM |
| validate_password_special_char_count | 1 |
+--------------------------------------+--------+
7 rows in set (0.00 sec)
#修改默认值为0
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
#修改密码默认最小长度为1
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
#再次修改密码,没有报错
mysql> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)