一、安装说明
本文使用二进制包安装及简单配置MySQL-v5.6.51版本。
5.6版本下载:
https://downloads.mysql.com/archives/community/
在此URL地址页面中的“Product Version”选择5.6的相应版本。
本文中使用本地VM虚机部署测试。
OS:CentOS Linux release 7.8.2003 (Core) 3.10.0-1160.15.2.el7.x86_64
虚机配置:2核CPU、4G内存
二、清理系统环境
CentOS 7 版本的系统默认自带安装了MariaDB,需要先清理。
## 查询已安装的mariadb
- rpm -qa |grep mariadb
## 卸载mariadb软件包,文件名为上述命令查询出来的文件
- rpm -e --nodeps mariadb-libs-5.5.65-1.el7.x86_64
三、创建mysql用户.组及数据目录
- ## 创建数据保存目录
- mkdir -p /data/mysql
- ## 创建用户组mysql
- groupadd -r mysql
- ## 创建用户mysql并设置不能登录系统,指定宿主目录
- useradd -g mysql -r -s /sbin/nologin -M -d /data/mysql mysql
- ## 修改数据保存目录属性及权限
- chown mysql.mysql /data/mysql
- chmod 0700 /data/mysql
四、安装配置MySQL
1.下载二进制包
- cd /data/tools
- wget https://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.51-linux-glibc2.12-x86_64.tar.gz
2.解压安装到指定目录(/usr/local)
- tar xf mysql-5.6.51-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
- cd /usr/local
- ## 创建软链接,方便后续操作
- ln -sv mysql-5.6.51-linux-glibc2.12-x86_64 mysql
3.创建配置文件
- vi /etc/my.cnf
- //输入以下内容
- [client]
- port = 3306
- socket = /usr/local/mysql/mysql.sock
- default-character-set = utf8mb4
- [mysqld]
- port = 3306
- socket = /usr/local/mysql/mysql.sock
- basedir = /usr/local/mysql
- datadir = /data/mysql
- character-set-server = utf8mb4
- collation-server = utf8mb4_general_ci
- init_connect = 'SET NAMES utf8mb4'
- server-id = 1
- log-slave-updates = true
- skip-external-locking
- skip-name-resolve
- key_buffer_size = 256M
- max_allowed_packet = 64M
- table_open_cache = 256
- sort_buffer_size = 16M
- read_buffer_size = 8M
- read_rnd_buffer_size = 64M
- myisam_sort_buffer_size = 256M
- thread_cache_size = 16
- query_cache_size = 128M
- query_cache_type = 1
- log-bin = mysql-bin
- binlog_format = row
- expire_logs_days = 30
- slow_query_log = on
- long_query_time = 2
- slow_query_log_file = /data/mysql/db-slow.log
- gtid-mode = on
- enforce-gtid-consistency = true
- default-storage-engine = INNODB
- innodb_additional_mem_pool_size = 64M
- innodb_buffer_pool_size = 1G
- innodb_data_file_path = ibdata1:10M:autoextend
- innodb_write_io_threads = 4
- innodb_read_io_threads = 4
- innodb_thread_concurrency = 8
- innodb_flush_log_at_trx_commit = 1
- innodb_log_buffer_size = 16M
- innodb_log_file_size = 512M
- innodb_log_files_in_group = 3
- innodb_max_dirty_pages_pct = 90
- innodb_lock_wait_timeout = 60
- innodb_file_per_table = on
- max_connections = 5000
- interactive_timeout = 28800
- wait_timeout = 28800
- sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
- explicit_defaults_for_timestamp = true
- [mysqldump]
- quick
- max_allowed_packet = 32M
- [mysql]
- no-auto-rehash
- default-character-set = utf8mb4
- [myisamchk]
- key_buffer_size = 128M
- sort_buffer_size = 128M
- read_buffer = 8M
- write_buffer = 8M
- [mysqlhotcopy]
- interactive-timeout
- [mysqld_safe]
- log-error = /data/mysql/mysql_err.log
- pid-file = /data/mysql/mysqld.pid
注:参数可根据实际环境需求进行修改,此处配置仅供参考。
4.修改程序目录权限
- cd /usr/local/mysql
- chown -R mysql.mysql .
5.初始化数据库
- cd /usr/local/mysql
- scripts/mysql_install_db --defaults-file=/etc/my.cnf --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
注:5.6版本使用mysql_install_db命令初始化数据,针对root@localhost用户不设置临时密码,客户端可以直接通过mysql命令登录数据库系统。
6.导出man帮助文件.头文件及lib库
- cd /usr/local/mysql
- ## 帮助文件
- echo "MANPATH /usr/local/mysql/man" >> /etc/man.config
- ## 头文件
- ln -sv /usr/local/mysql/include /usr/include/mysql
- ## lib库
- echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
- ## 加载动态库文件
- ldconfig
7.设置MySQL环境变量
- echo '######MySQL' >> /etc/profile
- echo 'PATH=$PATH:/usr/local/mysql/bin' >> /etc/profile
- source /etc/profile
8.配置MySQL服务管理命令
- cd /usr/local/mysql
- cp support-files/mysql.server /etc/rc.d/init.d/mysqld
9.启动数据库
- /etc/init.d/mysqld start
- /usr/local/mysql/bin/mysql --version && echo -e "\e[31m mysql install is OK\e[0m"
10.删除无用用户与测试库(可选操作)
- echo -e "delete from mysql.user where user='';" | mysql -uroot -p
- //提示输入密码,直接Enter键确认
- echo -e "delete from mysql.user where host='::1';" | mysql -uroot -p
- echo -e "drop database test;" | mysql -uroot -p
五、设置root账号密码
1.本机登录密码
- mysql -uroot -p
- //因初始化数据时没有设置密码,可以按两次确认键进入数据库
- mysql> use mysql;
- mysql> update user set password = PASSWORD('123456') where user = 'root';
- mysql> FLUSH PRIVILEGES;
2.远程登录密码
- # mysql -uroot -p
- //输入上述步骤中设置的密码登录数据库
- mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
- mysql> FLUSH PRIVILEGES;
六、服务管理
启动:/etc/init.d/mysqld start
关闭:/etc/init.d/mysqld stop
重启:/etc/init.d/mysqld restart
查看状态:/etc/init.d/mysqld status
配置开机启动:echo "/etc/init.d/mysqld start" >> /etc/rc.local