由于工作需要,需要在centos6.5服务器上安装mysql5.7,如下是详细安装过程。
一.安装环境
CentOS release 6.5 (Final)
Linux version 2.6.32-431.el6.x86_64
二.安装准备
需要下载mysql、boost, 并在yum中升级相关依赖包。
mysql
https://cdn.mysql.com/Downloads/MySQL-5.7/mysql-5.7.17.tar.gz
boost
https://jaist.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz
yum依赖
yum install -y gcc-c++ readline-devel zlib-devel bison cmake
yum install -y gcc-c++ readline-devel zlib-devel bison cmake
设置用户权限
groupadd mysql
useradd -g mysql mysql另外需要修改limits.conf
vim /etc/security/limits.conf
增加如下内容:
mysql soft nproc 16384
mysql hard nproc 16384
mysql soft nofile 65535
mysql hard nofile 65535
三.编译boost
boost是一个c++函数库,mysql5.7.5版本之后,必须安装boost,因此需要先编译boost。root用户下 ,将安装文件放在/opt/software目录。boost安装到/usr/local目录。
cd /opt/software/tar -zxvf boost_1_59_0.tar.gz -C /usr/localmv boost_1_59_0 boostcd boost/./bootstrap.sh./b2 install
四.安装mysql
由于服务器分区关系,所有软件都需要安装在/opt目录,因此mysql安装在/opt/mysql目录。
cd /opt/software/tar -zxvf mysql-5.7.17.tar.gzcd mysql-5.7.17/
预编译
[root@m162p49 mysql-5.7.17]# cmake \> -DCMAKE_INSTALL_PREFIX=/opt/mysql \> -DMYSQL_UNIX_ADDR=/opt/mysql/data/mysql.sock \> -DMYSQL_DATADIR=/opt/mysql/data \> -DDEFAULT_CHARSET=utf8 \> -DDEFAULT_COLLATION=utf8_general_ci \> -DWITH_MYISAM_STORAGE_ENGINE=1 \> -DWITH_INNOBASE_STORAGE_ENGINE=1 \> -DWITH_ARCHIVE_STORAGE_ENGINE=1 \> -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \> -DWITH_MEMORY_STORAGE_ENGINE=1 \> -DWITH_READLINE=1 \> -DENABLED_LOCAL_INFILE=1 \> -DMYSQL_USER=mysql \> -DMYSQL_TCP_PORT=3306 \> -DENABLE_DOWNLOADS=1 \> -DDOWNLOAD_BOOST=1 \> -DWITH_BOOST=/usr/local/boost
编译安装
预编译成功之后,可以进行make,时间比较长,至少30分钟以上。
make && make install
如下是make过程
至此,mysql编译和安装就成功了。
修改权限:
mkdir /opt/mysql/data
chown -R mysql:mysql /opt/mysql
五.新增配置文件
在/etc目录下,新增my.cnf文件,文件内容如下:
[client]port = 3306socket = /opt/mysql/data/mysql.sock[mysqld]port = 3306socket = /opt/mysql/data/mysql.sockbasedir = /opt/mysqldatadir = /opt/mysql/data#pid-file = /opt/mysql/mysql.piduser = mysqlbind-address = 0.0.0.0server-id = 1init-connect = 'SET NAMES utf8'character-set-server = utf8#skip-name-resolve#skip-networkingback_log = 300max_connections = 1000max_connect_errors = 6000open_files_limit = 65535table_open_cache = 128max_allowed_packet = 4Mbinlog_cache_size = 1Mmax_heap_table_size = 8Mtmp_table_size = 16Mread_buffer_size = 2Mread_rnd_buffer_size = 8Msort_buffer_size = 8Mjoin_buffer_size = 8Mkey_buffer_size = 4Mthread_cache_size = 8query_cache_type = 1query_cache_size = 8Mquery_cache_limit = 2Mft_min_word_len = 4log_bin = mysql-binbinlog_format = mixedexpire_logs_days = 30log_error = /opt/mysql/log/mysql-error.logslow_query_log = 1long_query_time = 1slow_query_log_file = /opt/mysql/log/mysql-slow.logperformance_schema = 0explicit_defaults_for_timestamp#lower_case_table_names = 1skip-external-lockingdefault_storage_engine = InnoDB#default-storage-engine = MyISAMinnodb_file_per_table = 1innodb_open_files = 500innodb_buffer_pool_size = 64Minnodb_write_io_threads = 4innodb_read_io_threads = 4innodb_thread_concurrency = 0innodb_purge_threads = 1innodb_flush_log_at_trx_commit = 2innodb_log_buffer_size = 2Minnodb_log_file_size = 32Minnodb_log_files_in_group = 3innodb_max_dirty_pages_pct = 90innodb_lock_wait_timeout = 120bulk_insert_buffer_size = 8Mmyisam_sort_buffer_size = 8Mmyisam_max_sort_file_size = 10Gmyisam_repair_threads = 1interactive_timeout = 28800wait_timeout = 28800[mysqldump]quickmax_allowed_packet = 16M[myisamchk]key_buffer_size = 8Msort_buffer_size = 8Mread_buffer = 4Mwrite_buffer = 4M
六.初始化数据库
cd /opt/mysql执行如下命令bin/mysqld --initialize --basedir=/opt/mysql --datadir=/opt/mysql/data --user=mysql
七.新增boot服务
cd /opt/mysqlcp support-files/mysql.server /etc/init.d/mysqldchmod +x /etc/init.d/mysqldchkconfig --add mysqld
八.启动数据库
service mysqld start
九.设置mysql环境变量
echo -e '\n\nexport PATH=/opt/mysql/bin:$PATH\n' >> /etc/profile && source /etc/profile
十.修改root密码
临时密码在日志中可以查看,如果没有配置日志或者无法找到临时密码,则需要通过如下步骤操作:
1.关闭mysql
service mysqld stop
2.修改MySQL授权登录方式
cd /opt/mysqlbin/mysqld_safe --skip-grant-tables &
之后登陆mysql,则不再需要密码。
3.切换到mysql库执行修改语句
mysql> use mysql;mysql> update user set password=password('') where user='root';ERROR 1054 (42S22): Unknown column 'password' in 'field list'
可见mysql5.7的用户密码与5.6不同,查看mysql的user表结构可知。
desc user;
select user,host,authentication_string,password_expired from user;
可以发现,除root用户外,还存在一个mysql.sys用户。此时root用户password_expired状态为Y。为已过期状态。密码字段变成了authentication_string 。
4.修改密码
update user set authentication_string=password('mysql') where user='root';flush privileges;exit
5.重启登陆
service mysqld restart
之后用重新修改的root密码即可登录。
6. 异常1820
登陆后如果查询数据库出现错误:
mysql> show databases;ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
则需要重新设置一下root密码
SET PASSWD = PASSWD('mysql');
重新登陆即可。
十一.创建数据库
现在mysql安装完毕,可以创建自己需要的数据库。
create database demo DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;grant all privileges on demo.* to demo@'%.%.%.%' identified by 'demo123';flush privileges;