1. 下载官网最新的带glibc库mysql社区版(目前是5.7.18)
shell> wget -P /usr/local https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
2. 安装依赖包
shell> yum install libaio (centos)
shell> apt-get install libaio1 (ubuntu)
3. 创建数据库配置文件
shell> touch /etc/my.cnf
基本配置的内容如下:
[client]
port = 10001
socket = /data/mysql/log/mysql.sock
[mysql]
prompt="u@mysqldb R:m:s [d]> "
no-auto-rehash
[mysqld]
user = mysql
port = 10001
basedir = /usr/local/mysql
datadir = /data/mysql/data
socket = /data/mysql/log/mysql.sock
pid-file = /data/mysql/log/mysql.pid
character-set-server = utf8
skip_name_resolve = 1
open_files_limit = 65535
back_log = 1024
max_connections = 512
max_connect_errors = 1000000
table_open_cache = 500
table_definition_cache = 500
table_open_cache_instances = 64
thread_stack = 512K
external-locking = FALSE
max_allowed_packet = 32M
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/log/slow.log
log-error = /data/mysql/log/error.log
long_query_time = 1
server-id = 10001
log-bin = /data/mysql/log/binlog
sync_binlog = 1
binlog_cache_size = 4M
max_binlog_cache_size = 2G
max_binlog_size = 1G
expire_logs_days = 7
master_info_repository = TABLE
relay_log_info_repository = TABLE
gtid_mode = on
enforce_gtid_consistency = 1
log_slave_updates
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
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
lock_wait_timeout = 3600
explicit_defaults_for_timestamp = 1
innodb_thread_concurrency = 0
innodb_sync_spin_loops = 100
innodb_spin_wait_delay = 30
transaction_isolation = REPEATABLE-READ
#innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 2048M
innodb_buffer_pool_instances = 8
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_data_file_path = ibdata1:1G: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_max_undo_log_size = 4G
innodb_io_capacity = 4000
innodb_io_capacity_max = 8000
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_file_format = Barracuda
#innodb_file_format_max = Barracuda
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
performance_schema = 1
performance_schema_instrument = '%=on'
#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
4. 搭建及初始化数据库
shell> groupadd mysql
shell> useradd -r -g mysql -s /bin/false mysql
shell> cd /usr/local
shell> tar zxvf /path/to/mysql-VERSION-OS.tar.gz
shell> ln -s full-path-to-mysql-VERSION-OS mysql (软连接,方便使用及升级)
shell> cd mysql
shell> mkdir mysql-files logs
# (/data 是数据库文件存放目录)
shell> mkdir /data
shell> chmod 750 mysql-files
shell> chown -R mysql .
shell> chgrp -R mysql .
shell> bin/mysql_install_db --user=mysql --user=mysql --basedir=/usr/local/mysql --datadir=/data # MySQL 5.7.5 and below
shell> bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data # MySQL 5.7.6 and up
shell> bin/mysql_ssl_rsa_setup # MySQL 5.7.6 and up (生成 ssl 证书)
shell> chown -R root .
shell> chown -R mysql mysql-files logs /data
5. 查看默认root密码
tail /usr/local/mysql/logs/mysqld.err
#找到类似如下的密码,并记录该root用户的临时密码
[Note] A temporary password is generated for root@localhost: Y;jA%jK=>7h2
6. 设置MySQL为系统服务并启动
shell> cd /usr/local/mysql
shell> cp support-files/mysql.server /etc/init.d/mysqld
# 更改服务启动文件的mysql安装目录
shell> sed -i s:basedir=s*$:basedir=/usr/local/mysql:g /etc/init.d/mysqld
# 更改服务启动文件的mysql数据库文件存放目录
shell> sed -i s:datadir=s*$:datadir=/data:g /etc/init.d/mysqld
shell> chkconfig --add mysqld
shell> chkconfig mysqld on
#启动mysql服务
shell> service mysqld start
7. 账户及密码安全设置
shell> cd /usr/local/mysql/bin
shell> ./mysql_secure_installation
# follow the instruction step by step 根据提示一步步操作
8. 输出MySQL操作命令目录为环境变量
for convinence 为了操作命令的方便,输出MySQL的bin目录至PATHshell> vi /etc/profile.d/mysql.sh
mysql.sh 内容如下:
if ! echo $PATH | /bin/grep -q /usr/local/mysql/bin ; then
PATH=${PATH}:/usr/local/mysql/bin
fi
export PATH
shell>chmod +x /etc/profile.d/mysql.sh