Home Database Mysql Tutorial 快速实现MySQL的部署以及一机多实例部署_MySQL

快速实现MySQL的部署以及一机多实例部署_MySQL

May 27, 2016 pm 01:44 PM

MySQL有三个版本:二进制,源码包,RPM。

下面讲讲二进制包的安装过程

下载地址:http://dev.mysql.com/downloads/mysql/

选择Linux-Generic

我这里选择的是mysql-5.6.28-linux-glibc2.5-x86_64.tar.gz

解压后,里面有个文件INSTALL-BINARY,其实给出了二进制包的部署过程


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> chown -R mysql .
shell> chgrp -R mysql .
shell> scripts/mysql_install_db --user=mysql
shell> chown -R root .
shell> chown -R mysql data
shell> bin/mysqld_safe --user=mysql &
# Next command is optional
shell> cp support-files/mysql.server /etc/init.d/mysql.server
Copy after login


相对于实际生产环境的部署,上面在初始化数据库的过程中少了一步-即指定配置文件,如果配置文件确认了,数据目录,日志目录都确认了,MySQL二进制版本的部署还是相当容易的一件事情。

下面写了一个脚本,基于后面提供的配置文件,执行格式如下:

sh 4.sh /root/mysql-5.6.28-linux-glibc2.5-x86_64.tar.gz /mysql3306 3306

其中 4.sh是脚本,/root/mysql-5.6.28-linux-glibc2.5-x86_64.tar.gz是二进制包的绝对路径,/mysql3306是basedir,3306是需设置的端口,

利用该脚本,只需要预先定义好配置文件,就可进行MySQL数据库的快速部署以及一台服务器上多个实例的部署。


#!/bin/bash
#需传入三个参数,第一个是mysql二进制压缩包的路径(绝对路径),
譬如/root/mysql-5.6.28-linux-glibc2.5-x86_64.tar.gz,
#第二个是mysql的basedir,即需要创建在哪个目录下,第三个是设置的端口号
filename=$1
basedir=$2
port=$3

groupadd mysql
useradd -r -g mysql -s /bin/false mysql
cd /usr/local
tar zxvf $filename
#file是获取mysql二进制包的名称,譬如mysql-5.6.28-linux-glibc2.5-x86_64.tar.gz
#dir是mysql压缩包的路径,不含包名本身,譬如/root,因为后续的配置文件my.cnf也是放到这个路径下
file=`basename $filename`
dir=`dirname $filename`

#获取解压后的名字,即mysql-5.6.28-linux-glibc2.5-x86_64
after_tar_file=${file:0:-7}
#将二进制包改名为 mysql+端口号,这样也便于后续的区分
mv $after_tar_file mysql"$port"
cd mysql"$port"

#将原始的配置文件(需和mysql压缩包放到同层目录下,在本例中是/root/my.cnf)
copy到解压并改名后的mysql二进制目录下,修改为my+端口号.cnf
cp $dir/my.cnf ./my"$port".cnf
user_cnf=my"$port".cnf
#下面主要是将原始配置文件中的路径修改为自己设定的路径,即传入的第二个参数
#整个的挑战在于传入的路径带有"/",在sed替换时会有问题,所有用了一个取巧的思路,即先将"/"替换为"|",
进行sed替换,然后再将文件中的"|"修改回"/"
basedir_new=${basedir/\//|}
sed -i "s/\/project\/class2/$basedir_new/g" $user_cnf
sed -i "s/|/\//g" $user_cnf

#设置server_id,取当前的秒值
server_id=`date +%s`
sed -i /^server_id/s/.*/server_id="$server_id"/ $user_cnf
#设置端口号
sed -i /^port/s/.*/port="$port"/ $user_cnf

#创建必要的目录并修改权限
mkdir -p "$basedir"/mysql/{run,data,share,log,tmp}

chown -R mysql $basedir
chgrp -R mysql $basedir

#下面这个是非必要的,具体看后面的总结
cp share/english/errmsg.sys "$basedir"/mysql/share/

#初始化时--force也是非必要的,具体可见后面的总结
scripts/mysql_install_db --user=mysql --defaults-file="$user_cnf" --force
bin/mysqld_safe --defaults-file="$user_cnf" --user=mysql &
Copy after login


下面给出了配置文件的一个参考,大家可根据实际情况进行相应的修改


[mysqld_safe]
pid-file=/project/class2/mysql/run/mysqld.pid

[mysql]
port=3306
prompt=\\u@\\d \\r:\\m:\\s>
default-character-set=utf8
no-auto-rehash

[client]
port=3306
socket=/project/class2/mysql/run/mysql.sock

[mysqld]
#dir
basedir=/project/class2/mysql
datadir=/project/class2/mysql/data
tmpdir=/tmp
lc_messages_dir=/project/class2/mysql/share
log-error=/project/class2/mysql/log/alert.log
slow_query_log_file=/project/class2/mysql/log/slow.log
general_log_file=/project/class2/mysql/log/general.log
socket=/project/class2/mysql/run/mysql.sock

#innodb
innodb_data_home_dir=/project/class2/mysql/data
innodb_log_group_home_dir=/project/class2/mysql/data
innodb_data_file_path=ibdata1:2G;ibdata2:16M:autoextend
innodb_buffer_pool_size=10G
innodb_buffer_pool_instances=4
innodb_log_files_in_group=2
innodb_log_file_size=1G
innodb_log_buffer_size=200M
innodb_flush_log_at_trx_commit=1
innodb_additional_mem_pool_size=20M
innodb_max_dirty_pages_pct=60
innodb_io_capacity=1000
innodb_thread_concurrency=16
innodb_read_io_threads=8
innodb_write_io_threads=8
innodb_open_files=60000
innodb_file_format=Barracuda
innodb_file_per_table=1
innodb_flush_method=O_DIRECT
innodb_change_buffering=inserts
innodb_adaptive_flushing=1
innodb_old_blocks_time=1000
innodb_stats_on_metadata=0
innodb_read_ahead=0
innodb_use_native_aio=0
innodb_lock_wait_timeout=5
innodb_rollback_on_timeout=0
innodb_purge_threads=1
innodb_strict_mode=1
transaction-isolation=READ-COMMITTED

#myisam
key_buffer=64M
myisam_sort_buffer_size=64M
concurrent_insert=2
delayed_insert_timeout=300

#replication
master-info-file=/project/class2/mysql/log/master.info
relay-log=/project/class2/mysql/log/relaylog
relay_log_info_file=/project/class2/mysql/log/relay-log.info
relay-log-index=/project/class2/mysql/log/mysqld-relay-bin.index
slave_load_tmpdir=/project/class2/mysql/tmp
slave_type_conversions="ALL_NON_LOSSY"
slave_net_timeout=4
skip-slave-start
sync_master_info=1000
sync_relay_log_info=1000

#binlog
log-bin=/project/class2/mysql/log/mysql-bin
server_id=2552763370
binlog_cache_size=32K
max_binlog_cache_size=2G
max_binlog_size=500M
binlog-format=ROW
sync_binlog=1000
log-slave-updates=1
expire_logs_days=0

#server
default-storage-engine=INNODB
character-set-server=utf8
lower_case_table_names=1
skip-external-locking
open_files_limit=65536
safe-user-create
local-infile=1
#sqlmod="STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE"

log_slow_admin_statements=1
log_warnings=1
long_query_time=1
slow_query_log=1
general_log=0

query_cache_type=0
query_cache_limit=1M
query_cache_min_res_unit=1K

table_definition_cache=65536

thread_stack=512K
thread_cache_size=256
read_rnd_buffer_size=128K
sort_buffer_size=256K
join_buffer_size=128K
read_buffer_size=128K

port=3306
skip-name-resolve
skip-ssl
max_connections=4500
max_user_connections=4000
max_connect_errors=65536
max_allowed_packet=128M
connect_timeout=8
net_read_timeout=30
net_write_timeout=60
back_log=1024
Copy after login


总结:

在初始化的过程中,如果报以下错误:


FATAL ERROR: Neither host 'keepalived02' nor 'localhost' could be looked up with
/mysql3306/mysql/bin/resolveip
Please configure the 'hostname' command to return a correct
hostname.
If you want to solve this at a later stage, restart this script
with the --force option
Copy after login


但是在bash终端上执行hostname命令又确实有值返回,可加--force参数,如下所示:


代码如下:

scripts/mysql_install_db --user=mysql --defaults-file="$user_cnf" --force
Copy after login

如果报以下错误:

代码如下:

[ERROR] Can't find messagefile '/mysql3306/mysql/share/errmsg.sys'
Copy after login

可将二进制版本中share/english/errmsg.sys文件COPY到/mysql3306/mysql/share/下。

后续:这两个错误的原因都是因为basedir修改了,它默认是在二进制包中查找的。

如何利用脚本实现MySQL的快速部署以及一机多实例的部署,通过这篇文章希望对大家学习MySQL的部署有所帮助。

以上就是快速实现MySQL的部署以及一机多实例部署_MySQL的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve mysql cannot be started How to solve mysql cannot be started Apr 08, 2025 pm 02:21 PM

There are many reasons why MySQL startup fails, and it can be diagnosed by checking the error log. Common causes include port conflicts (check port occupancy and modify configuration), permission issues (check service running user permissions), configuration file errors (check parameter settings), data directory corruption (restore data or rebuild table space), InnoDB table space issues (check ibdata1 files), plug-in loading failure (check error log). When solving problems, you should analyze them based on the error log, find the root cause of the problem, and develop the habit of backing up data regularly to prevent and solve problems.

Does mysql optimize lock tables Does mysql optimize lock tables Apr 08, 2025 pm 01:51 PM

MySQL uses shared locks and exclusive locks to manage concurrency, providing three lock types: table locks, row locks and page locks. Row locks can improve concurrency, and use the FOR UPDATE statement to add exclusive locks to rows. Pessimistic locks assume conflicts, and optimistic locks judge the data through the version number. Common lock table problems manifest as slow querying, use the SHOW PROCESSLIST command to view the queries held by the lock. Optimization measures include selecting appropriate indexes, reducing transaction scope, batch operations, and optimizing SQL statements.

How to use SUBSTRING_INDEX in MySQL How to use SUBSTRING_INDEX in MySQL Apr 08, 2025 pm 02:09 PM

In MySQL database operations, string processing is an inevitable link. The SUBSTRING_INDEX function is designed for this, which can efficiently extract substrings based on separators. SUBSTRING_INDEX function application example The following example shows the flexibility and practicality of the SUBSTRING_INDEX function: Extract specific parts from the URL For example, extract domain name: SELECTSUBSTRING_INDEX('www.mysql.com','.',2); Extract file extension to easily get file extension: SELECTSUBSTRING_INDEX('file.pdf','.',-1); Processing does not exist

Does mysql need the internet Does mysql need the internet Apr 08, 2025 pm 02:18 PM

MySQL can run without network connections for basic data storage and management. However, network connection is required for interaction with other systems, remote access, or using advanced features such as replication and clustering. Additionally, security measures (such as firewalls), performance optimization (choose the right network connection), and data backup are critical to connecting to the Internet.

Can mysql and mariadb coexist Can mysql and mariadb coexist Apr 08, 2025 pm 02:27 PM

MySQL and MariaDB can coexist, but need to be configured with caution. The key is to allocate different port numbers and data directories to each database, and adjust parameters such as memory allocation and cache size. Connection pooling, application configuration, and version differences also need to be considered and need to be carefully tested and planned to avoid pitfalls. Running two databases simultaneously can cause performance problems in situations where resources are limited.

The primary key of mysql can be null The primary key of mysql can be null Apr 08, 2025 pm 03:03 PM

The MySQL primary key cannot be empty because the primary key is a key attribute that uniquely identifies each row in the database. If the primary key can be empty, the record cannot be uniquely identifies, which will lead to data confusion. When using self-incremental integer columns or UUIDs as primary keys, you should consider factors such as efficiency and space occupancy and choose an appropriate solution.

Does mysql need a server Does mysql need a server Apr 08, 2025 pm 02:12 PM

For production environments, a server is usually required to run MySQL, for reasons including performance, reliability, security, and scalability. Servers usually have more powerful hardware, redundant configurations and stricter security measures. For small, low-load applications, MySQL can be run on local machines, but resource consumption, security risks and maintenance costs need to be carefully considered. For greater reliability and security, MySQL should be deployed on cloud or other servers. Choosing the appropriate server configuration requires evaluation based on application load and data volume.

Can mysql return json Can mysql return json Apr 08, 2025 pm 03:09 PM

MySQL can return JSON data. The JSON_EXTRACT function extracts field values. For complex queries, you can consider using the WHERE clause to filter JSON data, but pay attention to its performance impact. MySQL's support for JSON is constantly increasing, and it is recommended to pay attention to the latest version and features.

See all articles