Linux MySQL 安装
1, 下载
# 官网太慢,从国内镜像下载比较快
wget http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
2, 新建mysql用户
useradd mysql -m
-m
表示创建用户主目录,默认地址在/home下,可以通过 -d
指定。
3, 安装脚本
mysqlVersion=5.7.17
# 解压
tar zxvf /home/mysql/mysql-${mysqlVersion}-linux-glibc2.5-x86_64.tar.gz >>/home/mysql/setup.log 2>&1
# 移动
mv /home/mysql/mysql-${mysqlVersion}-linux-glibc2.5-x86_64 /usr/local/mysql-${mysqlVersion} >>/home/mysql/setup.log 2>&1
# 建立连接
ln -s /usr/local/mysql-${mysqlVersion} /usr/local/mysql
# 创建数据文件夹
mkdir -p /home/mysql/mysql-${mysqlVersion}/data >>/home/mysql/setup.log 2>&1
# 修改权限
chown -R mysql.mysql /home/mysql/mysql-${mysqlVersion} >>/home/mysql/setup.log 2>&1
# 备份旧配置,创建新配置文件
mv /etc/my.cnf /etc/bak_my.cnf >>/home/mysql/setup.log 2>&1
cp /home/mysql/my.cnf /etc/ >>/home/mysql/setup.log 2>&1
# 安装依赖包
yum install -y libaio* >>/home/mysql/setup.log 2>&1
# 初始化 mysql 基础数据
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/home/mysql/mysql-${mysqlVersion}/data/ >>/home/mysql/setup.log 2>&1
# 复制启动脚本
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld >>/home/mysql/setup.log 2>&1
# 开机启动
chkconfig mysqld on >>/home/mysql/setup.log 2>&1
# 启动mysql
service mysqld start >>/home/mysql/setup.log 2>&1
# 设置环境变量
echo 'PATH=$PATH:/usr/local/mysql/bin' >> /etc/profile
source /etc/profile >>/home/mysql/setup.log 2>&1
安装后,mysql 的默认密码会输出到 setup.log 中,可以在该文件中查找密码。
4, 完整的my.cnf
mysql5.7之后的安装包,已经没有配置模板文件,文件内容可以参见下面的内容,拷贝并保存成my.cnf即可。
基本配置
[mysqld]
port=3306
# 数据存储目录
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/mysql.sock
user=mysql
max_connections=151
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# 设置忽略大小写
lower_case_table_names = 1
# 指定编码
character-set-server=utf8mb4
# 开启ip绑定,0.0.0.0 表示任意ip可以访问
bind-address = 0.0.0.0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
#指定客户端连接mysql时的socket通信文件路径
[client]
socket=/usr/local/mysql/mysql.sock
# 客户端编码
default-character-set=utf8mb4
详细配置
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
port = 3306
socket = /tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
pid-file = /usr/local/mysql/data/mysql.pid
user = mysql
bind-address = 0.0.0.0
server-id = 9
character_set_server = utf8mb4
#innodb_io_capacity=600
#init-connect = 'SET NAMES utf8mb4'
skip-name-resolve
#skip-networking
back_log = 300
max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 4M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 4M
thread_cache_size = 8
query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M
ft_min_word_len = 4
#replicate-same-server-id
binlog_format = mixed
expire_logs_days = 20
log_error = /usr/local/mysql/log/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /usr/local/mysql/log/mysql-slow.log
performance_schema = 0
explicit_defaults_for_timestamp
#lower_case_table_names = 1
skip-external-locking
default_storage_engine = InnoDB
#default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 512M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 12G
myisam_repair_threads = 1
#interactive_timeout = 28800
interactive_timeout = 3600
wait_timeout = 7200
#wait_timeout = 28800
#rpl_semi_sync_master_enabled = 1
#rpl_semi_sync_slave_enabled = 1
log_bin_trust_function_creators=1
federated
group_concat_max_len=102400
sql_mode=NO_ENGINE_SUBSTITUTION,PIPES_AS_CONCAT,ANSI_QUOTES,STRICT_TRANS_TABLES
lower_case_table_names=1
[mysqldump]
quick
max_allowed_packet = 16M
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M