CentOS7.x安装MySQL5.7.25
mysql 5.7下载地址
社区版下载地址:https://dev.mysql.com/downloads/mysql/ 可能会有变动
找到5.7版本,
注:源码安装需要用到下面的包,可以先忽略,我安装是提示用到的不是这个版本的包,而且无法解压此包,安装提示中用到的是此包boost_1_59_0.tar.gz,可以连接外网的的情况下,能自动下载安装,没有也不要担心,会在报错页面,给出下载url地址,可以下载后本地上传至服务器中。
或者通过wget下载
wget http://downloads.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz
wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.12.tar.gz
环境准备
# cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core)
# uname -r
3.10.0-957.el7.x86_64
# getenforce
Disabled
# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
编译MySQL
解决依赖关系
# yum -y install autoconf automake libtool cmake ncurses-devel openssl-devel lzo-devel zlib-devel gcc gcc-c++ bison bison-devel git libaio-devel
注:下面所有的名利都要用绝对路径避免出错。
创建用户MySQL启动用户
groupadd mysql
useradd -r -g mysql -s /bin/false -M mysql
解压MySQL压缩包
# tar xf mysql-5.7.25
# cd mysql-5.7.25
进入解压目录并将boost_1_59_0.tar.gz放入mysql-5.7.25
# 创建文件目录
mkdir -p /application/mysql-5.7.25
编译安装MySQL
cmake -DCMAKE_INSTALL_PREFIX=/application/mysql-5.7.25/ -DSYSCONFDIR=/application/mysql-5.7.25/ -DMYSQL_DATADIR=/application/mysql-5.7.25/mysqldata -DMYSQL_UNIX_ADDR=/application/mysql-5.7.25/mysql.sock -DMYSQL_TCP_PORT=3306 -DMYSQL_USER=mysql -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DDOWNLOAD_BOOST=1 -DWITH_BOOST=.
-DCMAKE_INSTALL_PREFIX=/application/mysql-5.7.25/ #安装位置
-DSYSCONFDIR=/application/mysql-5.7.25/ #配置文件(my.cnf)目录
-DMYSQL_DATADIR=/application/mysqlData #数据存储目录
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock #mysql.sock目录
-DMYSQL_TCP_PORT=3306 #服务端口
-DMYSQL_USER=mysql #运行用户
-DEXTRA_CHARSETS=all #扩展字符支持
-DDEFAULT_CHARSET=utf8 #默认字符集
-DDEFAULT_COLLATION=utf8_general_ci #默认字符校对
-DWITH_READLINE=1 #readline库
-DENABLED_LOCAL_INFILE=1 #启用加载本地数据
-DWITH_MYISAM_STORAGE_ENGINE=1 #使用Myisam存储引擎
-DWITH_INNOBASE_STORAGE_ENGINE=1 #使用INNOBASE存储引擎
-DWITH_PARTITION_STORAGE_ENGINE=1 #安装数据库分区
-DWITH_MEMORY_STORAGE_ENGINE=1 #安装memory存储引擎
-DDOWNLOAD_BOOST=1 #下载升压文件boost_1_59_0.tar.gz
-DWITH_BOOST=. #指定boots文件位置,点表示当前位置,也可以用绝对路径,开始时因为将文件上传至此目录内,所以用点指定即可。
注:注意磁盘空间,不满足的情况下也是会编译失败的,最少给MySQL准备10G空间。
# make && make install
复制启动程序
cp support-files/mysql.server /etc/init.d/mysqld
注:centos7中,init.d下的启动文件要配置755权限,才可以使用,安装包中含有systemctl启动文件,可以用systemctl管理MySQL。
注:mysql。sock文件存放位置要设置成mysql:mysql用户,不然会提示没有sock文件。
设置mysql环境变量:
# echo "export PATH=/application/mysql/bin:/application/mysql/lib:$PATH" >>/root/.bash_profile
# source /root/.bash_profile
修改mysql权限
# chown -R mysql:mysql /application/*
创建软连接,MySQL升级后可以指定新的目录
# ln -s # ln -s /application/mysql-5.7.25 /application/mysql
配置my.cnf
# vim /etc/my.cnf
[client]
port = 3306
socket = /var/lib/mysql/mysql.sock
default-character-set = utf8
[mysqld]
port = 3306
socket = /var/lib/mysql/mysql.sock
basedir = /application/mysql
datadir = /application/mysqlDate
pid-file = /application/mysqlDate/mysql.pid
user = mysql
bind-address = 0.0.0.0
server-id = 1
init-connect = 'SET NAMES utf8mb4'
character-set-server = 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
log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 30
log_error = /application/mysqlDate/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /application/mysqlDate/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 = 64M
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 = 10G
myisam_repair_threads = 1
interactive_timeout = 28800
wait_timeout = 28800
[mysqldump]
quick
max_allowed_packet = 16M
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
启动MySQL
# /etc/init.d/mysqld restart
# ps -ef |grep mysql
root 11478 1 0 14:13 pts/0 00:00:00 /bin/sh /application/mysql/bin/mysqld_safe --datadir=/application/mysqlDate --pid-file=/application/mysqlDate/mysql.pid
mysql 12320 11478 8 14:13 pts/0 00:00:00 /application/mysql/bin/mysqld --basedir=/application/mysql --datadir=/application/mysqlDate --plugin-dir=/application/mysql/lib/plugin --user=mysql --log-error=/application/mysqlDate/mysql-error.log --open-files-limit=65535 --pid-file=/application/mysqlDate/mysql.pid --socket=/var/lib/mysql/mysql.sock --port=3306
# netstat -antup|grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 12320/mysqld
MySQL初始化
# MySQL5.7已经不推荐使用mysql_install_db初始化数据库了
进行一些安全设置,以及修改数据库管理员密码
# /application/mysql/bin/mysql_sceure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
注意:建议对所有Mariadb运行此脚本的所有部分生产中使用的服务器!请仔细阅读每一步!
In order to log into MariaDB to secure it, we'll need the currentpassword for the root user. If you've just installed MariaDB, andyou haven't set the root password yet, the password will be blank,so you should just press enter here.
Enter current password for root (enter for none): 回车即可
OK, successfully used password, moving on...
为了登录Mariadb以保护它,我们需要MariaDB根用户的密码。如果你刚刚安装了Mariadb,您尚未设置根密码,密码将为空,所以你只需在这里按回车键。
Setting the root password ensures that nobody can log into the MariaDBroot user without the proper authorisation.
Set root password? [Y/n]
设置根密码确保没有人可以登录mariadb未经适当授权的根用户。设置根密码?[y/n] #这里选择y,如果出现报错说明你没有用绝对路径,所有命令都是运行在据对路径下的,可以通过配置PATH来使命令生效。
By default, a MariaDB installation has an anonymous user, allowing anyoneto log into MariaDB without having to have a user account created forthem. This is intended only for testing, and to make the installationgo a bit smoother. You should remove them before moving into aproduction environment.
Remove anonymous users? [Y/n]
默认情况下,Mariadb安装有匿名用户,允许任何人登录Mariadb而不必为其创建用户帐户他们。仅用于测试和安装稍微平稳一点。在移动到生产环境。
删除匿名用户?[y/n] #这里y清除匿名用户。
Normally, root should only be allowed to connect from 'localhost'. Thisensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n]
通常,只允许根目录从“localhost”连接。这个确保有人无法猜测网络中的根密码。
不允许远程登录根目录?[y/n] # 这里选择y
By default, MariaDB comes with a database named 'test' that anyone canaccess. This is also intended only for testing, and should be removedbefore moving into a production environment.
Remove test database and access to it? [Y/n]
默认情况下,Mariadb附带一个名为“test”的数据库,任何人都可以访问该数据库。这也仅用于测试,在进入生产环境之前应将其移除。
删除测试数据库并访问它?[y/n] #这里选y
Reloading the privilege tables will ensure that all changes made so farwill take effect immediately.
Reload privilege tables now? [Y/n]、
重新加载特权表将确保所做的所有更改立即生效。
现在重新加载特权表?[y/n] #这里选择y
此时初始化及安全初始化就算做完了。
MySQL密码更改方法:
# ./mysqladmin -uroot -p password #enter
第一次输入原密码
第二次输入新密码
第三次验证新密码
登录进MySQL后输入下面命令
修改mysql的用户密码,分别使用grant、alter、set修改
l grant all on *.* to '用户名'@'登录主机' identified by '密码';
l alter user '用户名'@'登录主机' identified by '密码(自定义)';
l set password for 'root'@'localhost' = password('new password');
#所有修改密码操作,完成后都需要执行下边的命令
flush privileges;
忘记密码修改方法:
l 修改 /etc/my.cnf,在 [mysqld] 小节下添加一行:skip-grant-tables=1
l 这一行配置让 mysqld 启动时不对密码进行验证
l 重启 mysqld 服务:systemctl restart mysqld
l 使用 root 用户登录到 mysql:mysql -u root
l 切换到mysql数据库,更新 user 表:
l update user set authentication_string = password('root'), password_expired = 'N', password_last_changed = now() where user = 'root';
l 在之前的版本中,密码字段的字段名是 password,5.7版本改为了 authentication_string
l 退出 mysql,(exit或者quit)编辑 /etc/my.cnf 文件,删除 skip-grant-tables=1 的内容
l 重启 mysqld 服务,再用新密码登录即可