一. 准备工作
1 删除本地centos7中的mariadb:
查看系统中是否已安装 mariadb 服务:
rpm -qa | grep mariadb
或
yum list installed | grep mariadb
如果已安装则删除 mariadb及其依赖的包:
yum -y remove mariadb-libs-5.5.44-2.el7.centos.x86_64
关闭防火墙:
firewall-cmd --state #查看防火墙状态
systemctl stop firewalld.service #关闭防火墙
systemctl is-enabled firewalld.service;echo $? #查看防火墙是否开机自启
systemctl disable firewalld.service #禁止防火墙开机启动
MySQL依赖于libaio 库,安装libaio
yum search libaio
yum install libaio
2.下载安装包
mkdir /data
cd /data/
yum -y install wget
wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
3. 解压、重命名
tar -zxvf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz -C /data
mv /data/mysql-5.7.20-linux-glibc2.12-x86_64/ /data/mysql57
mkdir mysql #数据目录与安装目录分离,便于以后MySQL升级
二. 安装MySQL
1.添加用户和组
groupadd mysql
useradd mysql -g mysql
2.配置文件
/etc/my.cnf
主要参数及取值如下:
(相关取值,请根据实际环境做调整,如内存配置参数等)
#######
[client]
socket = /data/mysql/data/mysql.sock
default-character-set=utf8mb4
#loose-local-infile=
[mysqld]
log_bin_trust_function_creators= secure_file_priv='/tmp' ########dir settings########
basedir = /data/mysql57
datadir = /data/mysql/data
pid-file = /data/mysql/data/mysql.pid
socket = /data/mysql/data/mysql.sock
tmpdir = /data/mysql/tmp
log-bin = /data/mysql/mysql_log/mysql-bin
log-error = /data/mysql/mysql_log/mysql.err
general_log_file=/data/mysql/mysql_log/mysql.genlog
slow_query_log_file = /data/mysql/mysql_log/mysql_slow_new.log
relay_log = /data/mysql/mysql_log/relay.log ########server setting#######
#sql_mode = "ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
server-id =
port =
character-set-server=utf8mb4
collation_server=utf8mb4_unicode_ci
lower_case_table_names =
skip_name_resolve =
#max_connect_errors =
max_connections =
thread_cache_size=
#thread_stack=
#back_log=
max_allowed_packet =
event_scheduler =
local-infile=
# lower_case_table_names =
explicit_defaults_for_timestamp =
expire_logs_days =
log_bin_trust_function_creators = ####.frm/.ibd files qty related
open_files_limit=
innodb_open_files=
table_open_cache=
table_definition_cache= #### seesion buffer related
read_buffer_size =
read_rnd_buffer_size =
sort_buffer_size =
join_buffer_size = ####memory table size
tmp_table_size =
max_heap_table_size= ####timeout
interactive_timeout =
wait_timeout =
# connect_timeout= ########slow query ########
slow_query_log =
log_slow_slave_statements =
log_queries_not_using_indexes =
log_throttle_queries_not_using_indexes =
long_query_time =
#min_examined_row_limit = ########innodb settings########
innodb_buffer_pool_size =
innodb_buffer_pool_instances =
innodb_buffer_pool_dump_pct =
innodb_lru_scan_depth =
innodb_page_cleaners =
#innodb_purge_threads =
innodb_sort_buffer_size =
#innodb_file_per_table =
#innodb_flush_log_at_trx_commit = innodb_undo_log_truncate =
innodb_undo_tablespaces =
innodb_max_undo_log_size =
innodb_purge_rseg_truncate_frequency = innodb_log_file_size =
innodb_log_files_in_group =
innodb_log_buffer_size =
innodb_flush_method = O_DIRECT
innodb_flush_neighbors =
innodb_print_all_deadlocks = innodb_strict_mode =
#innodb_lock_wait_timeout = innodb_io_capacity =
innodb_io_capacity_max =
innodb_thread_concurrency =
innodb_write_io_threads =
innodb_read_io_threads = ########replication settings########
master_info_repository = TABLE
relay_log_info_repository = TABLE gtid_mode = on
enforce_gtid_consistency =
binlog_gtid_simple_recovery=
relay_log_recovery = slave-parallel-type = LOGICAL_CLOCK
slave-parallel-workers =
slave_transaction_retries=
slave_preserve_commit_order= log_slave_updates=
binlog_format = ROW
log_timestamps=system binlog_rows_query_log_events =
binlog_row_image='full'
slave_skip_errors = ddl_exist_errors ########semi sync replication settings########
##plugin_dir=/data/mysql/plugin/
#plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
#rpl_semi_sync_master_enabled =
#rpl_semi_sync_master_timeout =
#rpl_semi_sync_slave_enabled = [mysqldump]
quick
说明:
请一定记得上面 配置模板文件中的server-id 参数一定要修改。
否则在配置 主从复制,或者MHA时会报错。
例如错误信息 :
2018-10-09T16:13:53.205115+08:00 11 [ERROR] [MY-013117] [Repl] Slave I/O for channel '': Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it). Error_code: MY-013117
3. 创建目录并授权
cd /data/mysql
mkdir mysql_log
chown -R mysql.mysql mysql_log/
mkdir tmp
chown -R mysql.mysql tmp/
说明:
有时候,可能我们只是将MYSQL的二进制文件从其它的服务器Copy至本地。
例如我们在安装MYSQL8.0 时,安装文件从其它的服务,Copy到本地,文件命名为mysql80。如下图:
此时还需要执行以下权限,所以 我手动增加了一下权限,执行代码如下:
chmod -R 755 /data/mysql80/bin/
4.初始化MySQL
/data/mysql57/bin/mysqld --defaults-file=/etc/my.cnf --initialize --user=mysql --basedir=/data/mysql57 --datadir=/data/mysql/data
说明:
主参数--defaults-file=/etc/my.cnf必须紧接在mysqld后面,如果做第二个以后的参数加入时,则会出现报错:
[ERROR] unknown variable 'defaults-file=/etc/my.cnf'!
注意密码:5.7初始化后会生成一个临时密码,存在mysql.err
[Note] A temporary password is generated for root@localhost: XXXXXXXX
也就是root@localhost:后的字符串。
5.添加到MySQL启动脚本到系统服务,并且设置开启启动
cp -a /data/mysql57/support-files/mysql.server /etc/init.d/mysqld
设置开机启动和检查是否成功
chkconfig mysqld on
chkconfig --list | grep mysqld
6. 创建mysql软链接至/usr/bin下
ln -s /data/mysql57/bin/mysql /usr/bin
7.启动MySQL服务
service mysqld start
说明:
如果还是使用的Copy文件的方式,例如我们前面 Copy的mysql80 二进制文件, 此处就会报错:
此时 还要执行以下赋权命令 :
chmod -R 755 ./mysql.server (这个文件是 /data/mysql80/support-files)
和
chmod -R 755 /etc/init.d/mysqld
8. 登录MySQL,修改root密码
mysql -uroot -pXXXXXXX
mysql> set password=password('XXXXXXX2');
说明 :
MySQL 8.0 是报错的:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'password('XXXXX')' at line 1
在登录MySQL,修改root密码时,使用就方法 SET PASSWORD=PASSWORD(‘[新密码]’) 报错。
查看网上的相关解释为:
以前版本的MySQL的密码认证插件是“mysql_native_password”,而现在使用的是“caching_sha2_password”。
解决方案:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密码';
FLUSH PRIVILEGES;
三. 在本服务器上在开启一个实例
如果我们想在此服务器上在开始一个实例,例如 3307 用来测试。大体的安装步骤是一样的,但需要注意以下2点。
(1) 新实例的开启,可以通过defaults-file 参数开启,加入我们的参数文件是 my3307.cnf ,位于 /etc 目录下,那么 启动实例的脚本如下:
(mysql80 是代表80版本,上面的测试是5.7版本,所以是mysql57)
/data/mysql80/bin/mysqld --defaults-file=/etc/my3307.cnf –daemonize
(2) 开启后,主要怎么去连接。这个时候需要指定 socket。socket 一般位于mysql的数据目录中。实例初始化后,会产生账号密码,socket 也会生成,这些信息都可以在 mysql.err 文件中查看到。
本例的 socket 文件为/data/mysql_3307/data/mysql.sock,则多实例的登入方式如下:
mysql -S /data/mysql_3307/data/mysql.sock -uroot -p -P