一、删除之前可能有的 mariadb
(可选)
rpm -qa | grep mariadb
如果有就用yum remove -y xxxx
卸载
二、配置mysql5.7环境
官网: https://downloads.mysql.com/archives/community/
2.1 下载 安装
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.35-el7-x86_64.tar.gz
tar --no-same-owner -zxvf mysql-5.7.35-el7-x86_64.tar.gz -C /usr/local/
cd /usr/local/ && mv mysql-5.7.35-el7-x86_64 mysql
2.2 新建 mysql
用户组,用户
groupadd mysql
# -g:后面第一个是mysql是用户名,第二个是组名
useradd -r -s /sbin/nologin -g mysql mysql
2.3 创建数据目录并赋予权限
mkdir -p /data/{mysql,tmp}
chown -Rf mysql:mysql /data
2.4 .配置my.cnf
vim /etc/my.cnf
#--------------------------gtid模式主库配置------------------------
[client]
port = 3306
socket = /data/tmp/mysql.sock
default-character-set = utf8mb4
## ---------------- Basic ----------------
[mysqld]
port = 3306
socket = /data/tmp/mysql.sock
basedir = /usr/local/mysql/
datadir = /data/mysql
pid-file = /data/tmp/mysql.pid
log-error = /data/mysql/mysql-error.log
tmpdir = /data/tmp
user = mysql
character_set_server = utf8mb4
transaction_isolation = READ-COMMITTED
default-storage-engine = InnoDB
explicit_defaults_for_timestamp = 1
lower_case_table_names = 1
event_scheduler = 1
skip-external-locking
# ---------------- Connection/File/Table ----------------
interactive_timeout = 1800
wait_timeout = 1800
lock_wait_timeout = 1800
skip_name_resolve = 1
max_connections = 1000
max_user_connections = 256
max_connect_errors = 20000
back_log = 600
open_files_limit = 8192
table_open_cache = 4096
table_definition_cache = 4096
table_open_cache_instances = 64
# ---------------- Session Buffer Cache ----------------
query_cache_size = 0
query_cache_type = 0
max_allowed_packet = 64M
read_buffer_size = 16M
read_rnd_buffer_size = 16M
sort_buffer_size = 16M
tmp_table_size = 64M
join_buffer_size = 32M
thread_cache_size = 64
thread_stack = 256K
# ---------------- Binlog ----------------
log-bin = /data/tmp/mysql-bin
log-bin-index = /data/tmp/mysql-bin.index
binlog_format = row
expire_logs_days = 7
sync_binlog = 1
binlog_cache_size = 8M
max_binlog_cache_size = 2048M
max_binlog_size = 1024M
binlog_rows_query_log_events = 1
# ---------------- InnoDB ----------------
innodb_buffer_pool_size = 18G
innodb_buffer_pool_instances = 8
innodb_online_alter_log_max_size = 1024M
innodb_thread_concurrency = 8
innodb_io_capacity = 4000
innodb_io_capacity_max = 8000
innodb_sort_buffer_size = 64M
innodb_page_size = 16K
innodb_open_files = 4096
innodb_flush_log_at_trx_commit = 1
innodb_read_io_threads = 8
innodb_write_io_threads = 8
innodb_purge_threads = 4
innodb_max_dirty_pages_pct = 80
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_lock_wait_timeout = 10
innodb_rollback_on_timeout = 1
innodb_lru_scan_depth = 4096
innodb_flush_method = O_DIRECT
innodb_checksum_algorithm = CRC32
innodb_file_per_table = 1
innodb_file_format = Barracuda
innodb_file_format_max = Barracuda
innodb_flush_neighbors = 0
innodb_large_prefix = 1
innodb_print_all_deadlocks = 1
innodb_stats_persistent_sample_pages = 64
innodb_autoinc_lock_mode = 2
innodb_sync_spin_loops = 100
innodb_spin_wait_delay = 30
innodb_stats_on_metadata = 0
loose_innodb_numa_interleave = 1
innodb_buffer_pool_dump_pct = 40
innodb_page_cleaners = 4
innodb_undo_log_truncate = 1
innodb_max_undo_log_size = 2G
innodb_purge_rseg_truncate_frequency = 128
slave-parallel-type = LOGICAL_CLOCK
slave-parallel-workers = 16
slave_preserve_commit_order = 1
slave_transaction_retries = 128
binlog_gtid_simple_recovery = 1
log_timestamps = system
show_compatibility_56 = 1
# ---------------- Replication ----------------
server-id = 1 # 单机可不开启
master_info_repository = TABLE
relay_log_info_repository = TABLE
gtid_mode = on
enforce_gtid_consistency = 1
log_slave_updates = 1
relay_log = /data/mysql/relay.log
relay_log_recovery = 1
slave_skip_errors = ddl_exist_errors
slave-rows-search-algorithms = 'INDEX_SCAN,HASH_SCAN'
[mysqldump]
quick
max_allowed_packet=16M
single-transaction
[mysql]
no-auto-rehash
[mysqlhotcopy]
interactive-timeout
2.5 初始化数据库 --datadir 指定的目录必须为空
cd /usr/local/mysql/bin/
./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ \
--datadir=/data/mysql/ --user=mysql --initialize
成功的话是没有输出的:
2.6 查看初始化mysql
密码
如果没有密码,说明mysql初始化是失败的
grep 'temporary password' /data/mysql/mysql-error.log
#localhost:后面就是密码
A temporary password is generated for root@localhost: wxY+iF8!ak?r
2.7 优化命令,方便快速启动
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
2.8 启动mysql
/etc/init.d/mysqld start
三、修改 mysql 登入账号root的密码,并设置可以远程连接
3.1 修改登入账号root的密码(必须)
mysql -u root -p
mysql>SET PASSWORD = PASSWORD('123456');
mysql>ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
mysql>FLUSH PRIVILEGES;
3.2 设置可以远程连接,
刚搭建的是不能远程连接的,设置root用于可以远程连接
mysql>use mysql;
mysql>update user set host = '%' where user = 'root';
#使root能再任何host访问
mysql>FLUSH PRIVILEGES;