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 = 10001socket = /data/mysql/log/mysql.sock[mysql]prompt="\u@mysqldb \R:\m:\s [\d]> "no-auto-rehash[mysqld]user = mysqlport = 10001basedir = /usr/local/mysqldatadir = /data/mysql/datasocket = /data/mysql/log/mysql.sockpid-file = /data/mysql/log/mysql.pidcharacter-set-server = utf8skip_name_resolve = 1open_files_limit = 65535back_log = 1024max_connections = 512max_connect_errors = 1000000table_open_cache = 500table_definition_cache = 500table_open_cache_instances = 64thread_stack = 512Kexternal-locking = FALSEmax_allowed_packet = 32Msort_buffer_size = 4Mjoin_buffer_size = 4Mthread_cache_size = 768query_cache_size = 0query_cache_type = 0interactive_timeout = 600wait_timeout = 600tmp_table_size = 32Mmax_heap_table_size = 32Mslow_query_log = 1slow_query_log_file = /data/mysql/log/slow.loglog-error = /data/mysql/log/error.loglong_query_time = 1server-id = 10001log-bin = /data/mysql/log/binlogsync_binlog = 1binlog_cache_size = 4Mmax_binlog_cache_size = 2Gmax_binlog_size = 1Gexpire_logs_days = 7master_info_repository = TABLErelay_log_info_repository = TABLEgtid_mode = onenforce_gtid_consistency = 1log_slave_updatesbinlog_format = rowrelay_log_recovery = 1relay-log-purge = 1key_buffer_size = 32Mread_buffer_size = 8Mread_rnd_buffer_size = 4Mbulk_insert_buffer_size = 64Mmyisam_sort_buffer_size = 128Mmyisam_max_sort_file_size = 10Gmyisam_repair_threads = 1lock_wait_timeout = 3600explicit_defaults_for_timestamp = 1innodb_thread_concurrency = 0innodb_sync_spin_loops = 100innodb_spin_wait_delay = 30transaction_isolation = REPEATABLE-READ#innodb_additional_mem_pool_size = 16Minnodb_buffer_pool_size = 2048Minnodb_buffer_pool_instances = 8innodb_buffer_pool_load_at_startup = 1innodb_buffer_pool_dump_at_shutdown = 1innodb_data_file_path = ibdata1:1G:autoextendinnodb_flush_log_at_trx_commit = 1innodb_log_buffer_size = 32Minnodb_log_file_size = 2Ginnodb_log_files_in_group = 2innodb_max_undo_log_size = 4Ginnodb_io_capacity = 4000innodb_io_capacity_max = 8000innodb_write_io_threads = 8innodb_read_io_threads = 8innodb_purge_threads = 4innodb_page_cleaners = 4innodb_open_files = 65535innodb_max_dirty_pages_pct = 50innodb_flush_method = O_DIRECTinnodb_lru_scan_depth = 4000innodb_checksum_algorithm = crc32#innodb_file_format = Barracuda#innodb_file_format_max = Barracudainnodb_lock_wait_timeout = 10innodb_rollback_on_timeout = 1innodb_print_all_deadlocks = 1innodb_file_per_table = 1innodb_online_alter_log_max_size = 4Ginternal_tmp_disk_storage_engine = InnoDBinnodb_stats_on_metadata = 0innodb_status_file = 1innodb_status_output = 0innodb_status_output_locks = 0#performance_schemaperformance_schema = 1performance_schema_instrument = '%=on'#innodb monitorinnodb_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 mysqlshell> useradd -r -g mysql -s /bin/false mysqlshell> cd /usr/localshell> tar zxvf /path/to/mysql-VERSION-OS.tar.gzshell> ln -s full-path-to-mysql-VERSION-OS mysql (软连接,方便使用及升级)shell> cd mysqlshell> mkdir mysql-files logs# (/data 是数据库文件存放目录)shell> mkdir /data shell> chmod 750 mysql-filesshell> 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 belowshell> bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data # MySQL 5.7.6 and upshell> 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/mysqlshell> 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/mysqldshell> chkconfig --add mysqldshell> chkconfig mysqld on#启动mysql服务shell> service mysqld start
7. 账户及密码安全设置
shell> cd /usr/local/mysql/binshell> ./mysql_secure_installation# follow the instruction step by step 根据提示一步步操作
8. 输出MySQL操作命令目录为环境变量
for convinence 为了操作命令的方便,输出MySQL的bin目录至PATH
shell> vi /etc/profile.d/mysql.sh
mysql.sh 内容如下:
if ! echo $PATH | /bin/grep -q /usr/local/mysql/bin ; thenPATH=${PATH}:/usr/local/mysql/binfiexport PATH
shell>chmod +x /etc/profile.d/mysql.sh