search
HomeDatabaseMysql Tutorial[MySQL]MySQL的自动化安装部署_MySQL

有过MySQL运维的人应该都清楚,线上的MySQL一般都采用源码编译,因为这样才可以根据企业的各自需要选择要编译的功能,虽然MySQL的源码编译挺简单的,但是试想一下,如果你有几百台服务器同时要安装MySQL,难道你还一台台去手动编译、编写配置文件吗?这显然太低效了,本文讨论MySQL的自动化安装部署。

1、制作符合自己需求的RPM包

我们要根据MySQL的源码编译符合企业需求的RPM包,源码获取命令如下:
wget http://downloads.mysql.com/archives/get/file/mysql-5.6.16.tar.gztar -zxvf mysql-5.6.16.tar.gzcd mysql-5.6.16mkdir rpmcd rpm
在上面我们获取了源码,并在源码主目录下创建rpm目录,接着我们在该目录下创建mysql.spec文件:
Name: mysqlVersion:5.6.16Release: guahaoLicense: GPLURL: http://downloads.mysql.com/archives/get/file/mysql-5.6.16.tar.gzGroup: applications/databaseBuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root BuildRequires: cmakePackager: zhuxj@guahao.comAutoreq: noprefix: /opt/mysqlSummary: MySQL 5.6.16%description The MySQL(TM) software delivers a very fast, multi-threaded, multi-user,and robust SQL (Structured Query Language) database server. MySQL Serveris intended for mission-critical, heavy-load production systems as wellas for embedding into mass-deployed software.%define MYSQL_USER mysql%define MYSQL_GROUP mysql%define __os_install_post %{nil}%buildcd $OLDPWD/../CFLAGS="-O3 -g -fno-exceptions -static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing"CXX=g++CXXFLAGS="-O3 -g -fno-exceptions -fno-rtti -static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing"export CFLAGS CXX CXXFLAGScmake .                                                  /  -DSYSCONFDIR:PATH=%{prefix}                            /  -DCMAKE_INSTALL_PREFIX:PATH=%{prefix}                  /  -DCMAKE_BUILD_TYPE:STRING=Release                      /  -DENABLE_PROFILING:BOOL=ON                             /  -DWITH_DEBUG:BOOL=OFF                                  /  -DWITH_VALGRIND:BOOL=OFF                               /  -DENABLE_DEBUG_SYNC:BOOL=OFF                           /  -DWITH_EXTRA_CHARSETS:STRING=all                       /  -DWITH_SSL:STRING=bundled                              /  -DWITH_UNIT_TESTS:BOOL=OFF                             /  -DWITH_ZLIB:STRING=bundled                             /  -DWITH_PARTITION_STORAGE_ENGINE:BOOL=ON                /  -DWITH_INNOBASE_STORAGE_ENGINE:BOOL=ON                 /  -DWITH_ARCHIVE_STORAGE_ENGINE:BOOL=ON                  /  -DWITH_BLACKHOLE_STORAGE_ENGINE:BOOL=ON                /  -DWITH_PERFSCHEMA_STORAGE_ENGINE:BOOL=ON               /  -DDEFAULT_CHARSET=utf8                                 /  -DDEFAULT_COLLATION=utf8_general_ci                    /  -DWITH_EXTRA_CHARSETS=all                              /  -DENABLED_LOCAL_INFILE:BOOL=ON                         /  -DWITH_EMBEDDED_SERVER=0                               /  -DINSTALL_LAYOUT:STRING=STANDALONE                     /  -DCOMMUNITY_BUILD:BOOL=ON                              /  -DMYSQL_SERVER_SUFFIX='-r5436';make -j `cat /proc/cpuinfo | grep processor| wc -l`%installcd $OLDPWD/../make DESTDIR=$RPM_BUILD_ROOT install%cleanrm -rf $RPM_BUILD_ROOT%files%defattr(-, %{MYSQL_USER}, %{MYSQL_GROUP})%attr(755, %{MYSQL_USER}, %{MYSQL_GROUP}) %{prefix}/*%pre%postln -s %{prefix}/lib %{prefix}/lib64%preun%changelog
有了这个spec文件之后,就可以执行如下命令生成我们自己的RPM包:
rpmbuild -bb ./mysql.spec

2、编写my.cnf模板

my.cnf模板如下:
[mysqld_safe]pid-file=/opt/mysql/run/mysqld.pid[mysql]prompt=//u@//d //r://m://s>default-character-set=gbkno-auto-rehash[client]socket=/opt/mysql/run/mysql.sock[mysqld]#dirbasedir=/opt/mysqldatadir=/data/mysql/datatmpdir=/data/mysql/tmplog-error=/data/mysql/log/alert.logslow_query_log_file=/data/mysql/log/slow.loggeneral_log_file=/data/mysql/log/general.logsocket=/opt/mysql/run/mysql.sock#innodbinnodb_data_home_dir=/data/mysql/datainnodb_log_group_home_dir=/data/mysql/datainnodb_data_file_path=ibdata1:2G;ibdata2:16M:autoextendinnodb_buffer_pool_size=10Ginnodb_buffer_pool_instances=4innodb_log_files_in_group=4innodb_log_file_size=1Ginnodb_log_buffer_size=200Minnodb_flush_log_at_trx_commit=1innodb_additional_mem_pool_size=20Minnodb_max_dirty_pages_pct=60innodb_io_capacity=200innodb_thread_concurrency=32innodb_read_io_threads=8innodb_write_io_threads=8innodb_open_files=60000innodb_file_format=Barracudainnodb_file_per_table=1innodb_flush_method=O_DIRECTinnodb_change_buffering=allinnodb_adaptive_flushing=1innodb_old_blocks_time=1000innodb_stats_on_metadata=0innodb_read_ahead=0innodb_use_native_aio=0innodb_lock_wait_timeout=50innodb_rollback_on_timeout=0innodb_purge_threads=1innodb_strict_mode=1transaction-isolation=READ-COMMITTED#myisamkey_buffer_size=100Mmyisam_sort_buffer_size=64Mconcurrent_insert=2delayed_insert_timeout=300#replicationmaster-info-file=/data/mysql/log/master.inforelay-log=/data/mysql/log/mysql-relayrelay_log_info_file=/data/mysql/log/mysql-relay.inforelay-log-index=/data/mysql/log/mysql-relay.indexslave_load_tmpdir=/data/mysql/tmpslave_type_conversions="ALL_NON_LOSSY"slave_net_timeout=4skip-slave-startsync_master_info=1000sync_relay_log_info=1000#binloglog-bin=/data/mysql/log/mysql-binserver_id=2552763370binlog_cache_size=32Kmax_binlog_cache_size=2Gmax_binlog_size=500Mbinlog_format=ROWsync_binlog=1000log-slave-updates=1expire_logs_days=0#serverdefault-storage-engine=INNODBcharacter-set-server=gbklower_case_table_names=1skip-external-lockingopen_files_limit=65536safe-user-createlocal-infile=1performance_schema=0log_slow_admin_statements=1log_warnings=1long_query_time=1slow_query_log=1general_log=0query_cache_type=0query_cache_limit=1Mquery_cache_min_res_unit=1Ktable_definition_cache=65536thread_stack=512Kthread_cache_size=256read_rnd_buffer_size=128Ksort_buffer_size=256Kjoin_buffer_size=128Kread_buffer_size=128Kport=3306skip-name-resolveskip-sslmax_connections=4500max_user_connections=4000max_connect_errors=65536max_allowed_packet=128Mconnect_timeout=8net_read_timeout=30net_write_timeout=60back_log=1024#server id
细心的读者应该会注意在,在my.cnf的末尾在server id上留了空白,在后面的shell脚本会动态加上,这是因为在一个企业内部的所有MySQL的server id必须保持全局一致性,这样在主备复制时才不会导致混乱。其实如果想把这个脚本写的更通用,完全可以把更多的参数留白,如port、datadir、内存相关参数等,这里我只是以server id为例,抛砖引玉。

3、准备MySQL数据目录模板

你得事先准备一台MySQL,可以根据自己的需求,把通用性的东西放在上面(如账户等),下面是一个最简单的已安装好的MySQL的数据目录结构:
[root@lx25 mysql]# ls -ltotal 12drwxr-xr-x 5 mysql mysql 4096 Jul  2 09:26 datadrwxr-xr-x 2 mysql mysql 4096 Jul  1 18:21 logdrwxr-xr-x 2 mysql mysql 4096 Jul  2 09:26 tmp[root@lx25 mysql]# cd data[root@lx25 data]# ls -ltotal 6314044drwx------ 2 mysql mysql       4096 Jul  1 17:17 mysqldrwx------ 2 mysql mysql       4096 Jul  1 17:17 performance_schemadrwx------ 2 mysql mysql       4096 Jul  1 17:17 test
把该目录用tar打包(命名为data.tar),然后以这个为模板解压至新装MySQL实例的数据目录下即可。

4、编写自动化安装部署脚本

在运行这个脚本之前,我们必须得把前面几部制作的rpm包、my.cnf模板和数据目录模板放到一个固定的地方,本例中是放在企业内部的ftp上。MySQL自动化安装部署脚本(命名为:mysql_install.sh)如下:
#!/bin/sh#Step 1: Prepareyum install cmake gcc g++ bison ncurses-devel zlib groupadd mysqluseradd -g mysql mysql#Step 2: Get Sourceftp -n<<EOFopen 10.10.100.254user zhuxianjie zxj321binarycd mysqlpromptmget *EOF#Step 3: Installunique_id=`date "+%Y%m%d%M%S"`echo &#39;server_id=&#39;$unique_id >> my.cnfrpm -ivh mysql-5.6.16-guahao.x86_64.rpmcp my.cnf /opt/mysqlchown -R mysql:mysql /opt/mysqltar xvf data.tar -C /datachown -R mysql:mysql /data/mysql#step 4: Start MySQLcp /opt/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqldchmod 755 /etc/init.d/mysqldchkconfig mysqld on/etc/init.d/mysqld start


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)