1.mysql下载
官网下载:https://dev.mysql.com/downloads/mysql/
这里说明一下:通过作为DBA一般会下载Linux-Generic版本进行安装(Linux操作系统中),除非有特殊的场景需要用源码包安装,否则95%以上的都用二进制包安装,因为官方已有说明进行官方编译器编译后的二进制包安装的Mysql性能不会比人工手动编译的源码包安装的差!
2.mysq安装
这里采用自己写的脚本脚本安装
#!bin/bash
yum remove maridb*
mysql_user=`grep mysql /etc/passwd|awk -F ":" '{print $1}'`
mysql_group=`grep mysql /etc/group|awk -F ":" '{print $1}'`
if [[ "mysql"==${mysql_user} && "mysql_group"==${mysql_group} ]]; then
echo "mysql 用户和用户组存在"
else
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
fi
cd /usr/local && rm -rf mysql
cd /usr/local/src && rm -rf *
cd /usr/local/src && wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.25-linux-glibc2.17-x86_64-minimal.tar.xz
tar -xf mysql-8.0.25-linux-glibc2.17-x86_64-minimal.tar.xz
mv mysql-8.0.25-linux-glibc2.17-x86_64-minimal mysql
rm -rf mysql-8.0.25-linux-glibc2.17-x86_64-minimal.tar.xz
ln -s /usr/local/src/mysql /usr/local/mysql
chown -R mysql:mysql /usr/local/src/mysql
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
if [[ -d /mysql_data ]]; then
cd /mysql_data && rm -rf *
else
mkdir -p /mysql_data
fi
mkdir -p /mysql_data/mysql
mkdir -p /mysql_data/mysql/data
mkdir -p /mysql_data/mysql/binlog
mkdir -p /mysql_data/mysql/slow_log
chown -R mysql:mysql /mysql_data
cat >/root/.bash_profile <<EOF
MYSQL_BASE=/usr/local/mysql
MYSQL_DIR=/mysql_data/mysql/data
export PATH=\$PATH:\${MYSQL_BASE}/bin
EOF
source /root/.bash_profile
cat >/etc/my.cnf <<EOF
[client]
port = 3306
socket = /mysql_data/mysql/data/mysql.sock
[mysql]
prompt = "\u@mysqldb \R:\m:\s [\d]> "
no_auto_rehash
loose-skip-binary-as-hex
[mysqld]
user = mysql
port = 3306
server_id = 3306
basedir = /usr/local/mysql
datadir = /mysql_data/mysql/data
socket = /mysql_data/mysql/data/mysql.sock
pid_file = mysqldb.pid
character_set_server = UTF8MB4
skip_name_resolve = 1
admin_address = '127.0.0.1'
admin_port = 33062
lock_wait_timeout = 3600
open_files_limit = 65535
back_log = 1024
max_connections = 512
max_connect_errors = 1000000
table_open_cache = 1024
table_definition_cache = 1024
thread_stack = 512K
sort_buffer_size = 4M
join_buffer_size = 4M
read_buffer_size = 8M
read_rnd_buffer_size = 4M
bulk_insert_buffer_size = 64M
thread_cache_size = 768
interactive_timeout = 600
wait_timeout = 600
tmp_table_size = 32M
max_heap_table_size = 32M
log_timestamps = SYSTEM
log_error = /mysql_data/mysql/data/error.log
log_error_verbosity = 3
slow_query_log = 1
log_slow_extra = 1
slow_query_log_file = /mysql_data/mysql/slow_log/slow.log
long_query_time = 0.1
log_queries_not_using_indexes = 1
log_throttle_queries_not_using_indexes = 60
min_examined_row_limit = 100
log_slow_admin_statements = 1
log_slow_slave_statements = 1
log_bin = /mysql_data/mysql/binlog/mybinlog
binlog_format = ROW
sync_binlog = 1 #MGR环境中由其他节点提供容错性,可不设置双1以提高本地节点性能
binlog_cache_size = 4M
max_binlog_cache_size = 2G
max_binlog_size = 1G
binlog_rows_query_log_events = 1
binlog_expire_logs_seconds = 604800
#MySQL 8.0.22前,想启用MGR的话,需要设置binlog_checksum=NONE才行
binlog_checksum = CRC32
gtid_mode = ON
enforce_gtid_consistency = TRUE
#myisam settings
key_buffer_size = 32M
myisam_sort_buffer_size = 128M
#replication settings
relay_log_recovery = 1
slave_parallel_type = LOGICAL_CLOCK
slave_parallel_workers = 64 #可以设置为逻辑CPU数量的2倍
binlog_transaction_dependency_tracking = WRITESET
slave_preserve_commit_order = 1
slave_checkpoint_period = 2
replication_optimize_for_static_plugin_config = ON
replication_sender_observe_commit_only = ON
#mgr settings
loose-plugin_load_add = 'mysql_clone.so'
loose-plugin_load_add = 'group_replication.so'
loose-group_replication_group_name = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaa1"
#MGR本地节点IP:PORT,请自行替换
loose-group_replication_local_address = "172.16.16.10:33061"
#MGR集群所有节点IP:PORT,请自行替换
loose-group_replication_group_seeds = "172.16.16.10:33061,172.16.16.11:33061,172.16.16.12:33061"
loose-group_replication_start_on_boot = OFF
loose-group_replication_bootstrap_group = OFF
loose-group_replication_exit_state_action = READ_ONLY
loose-group_replication_flow_control_mode = "DISABLED"
loose-group_replication_single_primary_mode = ON
loose-group_replication_communication_max_message_size = 10M
loose-group_replication_unreachable_majority_timeout = 30
loose-group_replication_member_expel_timeout = 5
loose-group_replication_autorejoin_tries = 288
#innodb settings
transaction_isolation = REPEATABLE-READ
innodb_buffer_pool_size = 1024M # 根据实际需要进行调整,一般为物理内存的百分之80
innodb_buffer_pool_instances = 4
innodb_data_file_path = ibdata1:12M:autoextend
innodb_flush_log_at_trx_commit = 1 #MGR环境中由其他节点提供容错性,可不设置双1以提高本地节点性能
innodb_log_buffer_size = 32M
innodb_log_file_size = 1G #如果线上环境的TPS较高,建议加大至1G以上,如果压力不大可以调小
innodb_log_files_in_group = 3
innodb_max_undo_log_size = 4G
# 根据您的服务器IOPS能力适当调整
# 一般配普通SSD盘的话,可以调整到 10000 - 20000
# 配置高端PCIe SSD卡的话,则可以调整的更高,比如 50000 - 80000
innodb_io_capacity = 4000
innodb_io_capacity_max = 8000
innodb_open_files = 65535
innodb_flush_method = O_DIRECT
innodb_lru_scan_depth = 4000
innodb_lock_wait_timeout = 10
innodb_rollback_on_timeout = 1
innodb_print_all_deadlocks = 1
innodb_online_alter_log_max_size = 4G
innodb_print_ddl_logs = 1
innodb_status_file = 1
#注意: 开启 innodb_status_output & innodb_status_output_locks 后, 可能会导致log_error文件增长较快
innodb_status_output = 0
innodb_status_output_locks = 1
innodb_sort_buffer_size = 67108864
innodb_adaptive_hash_index = OFF
#提高索引统计信息精确度
innodb_stats_persistent_sample_pages = 500
#innodb monitor settings
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"
#pfs settings
performance_schema = 1
#performance_schema_instrument = '%memory%=on'
loose-performance_schema_instrument = '%lock%=on'
[mysqldump]
quick
EOF
mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/mysql_data/mysql/data
echo "mysql server instance 初始化完成,请启动mysql server!!!"
View Code
3.mysql初始化问题
在5.6版本中一般使用的是脚本中mysql_install_db命令进行初始化数据库 :mysql_install_db --user=mysql --basedir=xxxxxxxx --datadir=xxxxx
在5.7和8.0版本使用的是命令mysqld进行初始数据库: mysqld --initialize-insecure --user=mysql --basedir=xxxxxxxx --datadir=xxxxx
--initialize-insecure 这边表示初始时不生成密码,如果用的是--initialize的话表示要生成临时密码,生成的临时密码一般会在错误日志中找到。
4.mysql的配置文件
这里不说了,后面的博客都写有
5.mysql启动
通常myql启动有两种方式,一种是脚本启动,一种是用systemd管理方式进行启动
1) 脚本启动:将软件中的mysql目录下有个support-files目录,将里面的mysql.server文件复制到/etc/init.d下面并改名mysqld
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
然后启动 /etc/init.d/mysqld start 或者用命令 service mysqld start|stop|status
2) systemd管理方式
在/etc/systemd/system目录下 命名为mysqld.service,然后写入内容:
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/3307/my.cnf //只修改此处路径即可,安装路径根据自己的情况而定。
LimitNOFILE = 5000
6.mysql安全加固
3.1修改初始密码(此处针对临时密码和空密码的情况)
1) 进入数据库中用alter user命令进行修改
2) 使用mysqladmin命令进行修改: mysqladmin -uroot -pxxx
3.2 删除非root或非localhost的用户
具体可以通过select user,host from mysql.user 命令可以看当前Mysql用户状态
delete from mysql.user where user !='root' or host !='localhost' ; ##删除非root或者非localhost的用户
7.数据库密码忘记root怎么处理?
1)关闭数据库 2)修改配置文件/etc/my.cnf 中添加:skip-grant-tables 3)启动数据库后进入执行flush privileges 4)修改root密码:alter user root@'%' identified by '密码’;
8.mysqld_safe和mysqld启动方式不同?
这里补充一下:关于mysqld_safe 和mysqld启动的不同
- 首先直接用mysqld命令启动mysql server时,在linux后台会发现关于mysqld进程只有一个,而用mysql_safe启动mysql server时,在linux后台会发现关于mysqld进程有两个
- 直接运行mysqld程序来启动MySQL服务的方法很少见,mysqld_safe脚本会在启动MySQL服务器后继续监控其运行情况,并在其死机时重新启动它。
mysqld_safe其实主要做如下几件事
- 检查系统和选项
- 检查MyISAM表
- 保持MySQL服务器窗口
- 启动并监视mysqld,如果因错误终止则重启。
- 将mysqld的错误消息发送到数据目录中的host_name.err 文件。
- 将mysqld_safe的屏幕输出发送到数据目录中的host_name.safe文件。
【mysqld】:是mysql的核心程序,用于管理mysql的数据库文件以及用户的请求操作。mysqld可以读取配置文件中的[mysqld]的部分
【mysqld_safe】:相当于多了一个守护进程,mysqld挂了会自动把mysqld进程拉起来