从今天开始MySQL相关方面的东西
今天是关于MySQL的安装
系统为 redhat 6.10
数据库为MySQL 5.7.21
PS:目前最新版本为MySQL 5.7.25
1. 目录规划
2. MySQL 5.7 下载
目前MySQL 社区的GA 提供5 和8 的下载
由于目前大多数系统用的还是5的版本
这里统一使用5的版本
注意,下面是按照5.7.21的版本安装,5.25相同
3. 新建用户及目录
/usr/sbin/groupadd -g 105 mysql
/usr/sbin/useradd -u 105 -g mysql mysql
echo "mysql123" |passwd mysql --stdin
4. 新建目录mkdir -p /data/mysql/software
mkdir -p /usr/local/mysql
mkdir -p /data/mysql/data
mkdir -p /datalog/mysql/binlog
mkdir -p /datalog/mysql/relaylog
chown -R mysql:mysql /usr/local/mysql
mkdir -p /data/mysql/tmp
chown -R mysql:mysql /data/mysql
chown -R mysql:mysql /datalog/mysql/
5. 配置环境变量su - mysql
vim ~/.bash_profile
export MYSQL_HOME=/usr/local/mysql
export PATH=$HOME/bin:$MYSQL_HOME/bin:$PATH
export LD_LIBRARY_PATH=$MYSQL_HOME/lib:$LD_LIBRARY_PATH
source ~/.bash_profile
6. 建立配置文件
我们这里建立 /etc/my.cnf[mysql]
user =
password =
[mysqld]
#-----------------MySQL Basic Setting-----------------#
server-id = 1723161113
port = 3306
user = mysql
pid-file = mysql.pid
character_set_server = utf8mb4
default_storage_engine = InnoDB
skip_name_resolve = 1
lower_case_table_names = 1
explicit_defaults_for_timestamp = 1
open_files_limit = 65535
max_connections = 1000
max_connect_errors = 100000
basedir = /usr/local/mysql
datadir = /data/mysql/data
tmpdir = /data/mysql/tmp
socket = /data/mysql/data/mysql.sock
query_cache_type = 0
query_cache_size = 0
join_buffer_size = 64M
tmp_table_size = 64M
max_allowed_packet = 32M
read_buffer_size = 16M
read_rnd_buffer_size = 32M
sort_buffer_size = 32M
#-----------------MySQL Log Setting-----------------#
log_error = mysql-error.log
log_bin = /datalog/mysql/binlog/mysql-bin.log
slow_query_log_file = mysql-slow.log
relay_log = /datalog/mysql/relaylog/mysql-relay.log
log_slave_updates = 1
sync_binlog = 1
relay_log_recovery = 1
binlog_format = row
expire_logs_days = 14
slow_query_log = 1
long_query_time = 2
log_queries_not_using_indexes = 1
log_throttle_queries_not_using_indexes = 10
log_slow_admin_statements = 1
log_slow_slave_statements = 1
min_examined_row_limit = 1000
#-----------------MySQL Replication Setting-----------------#
slave_skip_errors = ddl_exist_errors
master_info_repository = TABLE
relay_log_info_repository = TABLE
gtid_mode = on
enforce_gtid_consistency = 1
binlog_rows_query_log_events = 1
#-----------------MySQL InnoDB Setting-----------------#
innodb_page_size = 16384
innodb_buffer_pool_size = 25600M
innodb_data_file_path = ibdata1:1G:autoextend
innodb_buffer_pool_instances = 8
innodb_file_per_table = 1
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 5
innodb_io_capacity = 800
innodb_io_capacity_max = 2000
innodb_flush_method = O_DIRECT
innodb_file_format = Barracuda
innodb_file_format_max = Barracuda
innodb_undo_logs = 128
innodb_undo_tablespaces = 3
innodb_flush_neighbors = 1
innodb_log_file_size = 2G
innodb_log_buffer_size = 16777216
innodb_print_all_deadlocks = 1
innodb_strict_mode = 1
innodb_sort_buffer_size = 67108864
#-----------------MySQL semi Replication Setting-----------------#
#plugin_dir = /usr/local/mysql/lib/plugin
#plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
#loose_rpl_semi_sync_master_enabled = 1
#loose_rpl_semi_sync_slave_enabled = 1
#loose_rpl_semi_sync_master_timeout = 5000
修改my.cnf权限
chown mysql:mysql /etc/my.cnf
7. 依赖包检查libaio-0.3.107-10.el6.x86_64
libaio-devel-0.3.107-10.el6.x86_64
lvm2-2.02.143-7.el6_8.1.x86_64
8. 卸载以前版本
如果有以前版本的MySQL ,则需要先下载
如只有lib库则可以不用动
RedHat Enterprise Linux 6适用】# rpm -qa|grep -i mysql
mysql-libs-5.1.71-1.el6.x86_64
qt-mysql-4.6.2-26.el6_4.x86_64
mysql-5.1.71-1.el6.x86_64
mysql-server-5.1.71-1.el6.x86_64
#yum remove mysql-5.1.71-1.el6.x86_64 qt-mysql-4.6.2-26.el6_4.x86_64 mysql-libs-5.1.71-1.el6.x86_64 mysql-server-5.1.71-1.el6.x86_64
【RedHat Enterprise Linux 7适用】# rpm -qa|grep -i mariadb*
mariadb-server-5.5.52-1.el7.x86_64
mariadb-libs-5.5.52-1.el7.x86_64
mariadb-embedded-5.5.52-1.el7.x86_64
mariadb-test-5.5.52-1.el7.x86_64
mariadb-bench-5.5.52-1.el7.x86_64
mariadb-5.5.52-1.el7.x86_64
mariadb-devel-5.5.52-1.el7.x86_64
mariadb-embedded-devel-5.5.52-1.el7.x86_64
# yum remove -y mariadb*
9. 解压文件
9.1 解压安装文件su - mysql
tar xzvf mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz -C /usr/local/mysql/ --strip-components=1
9.2 配置服务文件
这里将mysql.server文件拷贝值init.d目录使其可以当作服务启停cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
这里修改下面几处vim /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/data/mysql/data
lockdir='/data/mysql/data'
mysqld_pid_file_path=/data/mysql/data/mysql.pid
10. 初始化数据库
接下来我们初始化MySQL#su - mysql
$cd /usr/local/mysql
$/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --initialize --user=mysql
初始化过程中可以通过error文件查看进度
tail -f /data/mysql/data/mysql-error.log
11. 启动数据库$ /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql &
$ netstat -lntp|grep mysqld
后续可以使用如下命令启停数据库
service mysqld stop
service mysqld start
service mysqld restart
设置MySQL自启动
chkconfig mysqld on
12. 连接数据库
默认密码在error文件中有
cat /data/mysql/data/mysql-error.log |grep password
使用如下命令连接
mysql -S /data/mysql/data/mysql.sock -uroot -ppassword
连接后的安全性设置参见MySQL常见命令
好了 MySQL的 Linux 安装就先讲到这里
后续内容尽情期待