由于工作需要,需要在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/mysqld?chkconfig --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;