2017-12-26--mysql(5.6.15),linux下安裝使用
本文檔的目的是,指導使用者安裝、配置、還原所需要用到的mysql數據庫。僅提供linux版本服務器的安裝說明,且linux版本為64位的Centos6.4。同時,會提供的mysql安裝包(MySQL-5.6.15-1.linux_glibc2.5.x86_64.rpm-bundle.tar)和還原數據庫所需用到的sql腳本。
mysql安裝提供的tar包,解壓到/opt/mysql(實際使用可變換目錄)目錄下。解壓後,會出現如下幾個rpm包,我們只用到其中紅色字體的兩個rpm包。
[root@localhost mysql]# tar -xvf MySQL-5.6.15-1.linux_glibc2.5.x86_64.rpm-bundle.tar
MySQL-devel-5.6.15-1.linux_glibc2.5.x86_64.rpm
MySQL-embedded-5.6.15-1.linux_glibc2.5.x86_64.rpm
MySQL-test-5.6.15-1.linux_glibc2.5.x86_64.rpm
MySQL-server-5.6.15-1.linux_glibc2.5.x86_64.rpm
MySQL-shared-5.6.15-1.linux_glibc2.5.x86_64.rpm
MySQL-client-5.6.15-1.linux_glibc2.5.x86_64.rpm
MySQL-shared-compat-5.6.15-1.linux_glibc2.5.x86_64.rpm
安裝步驟,如下說明:
一、檢查環境,刪除已有的舊版本
l 檢查mysql,如已存在則需要刪除。執行腳本如下
rpm -qa mysql
rpm -qa mysql-5.1.66-2.el6_3.x86_64
rpm -e mysql --nodeps;
l 檢查mysql-server,如已存在則需要刪除。執行腳本如下
rpm -qa mysql-server
mysql-server-5.1.66-2.el6_3.x86_64
rpm -e mysql-server --nodeps;
l 檢查mysql- client,如已存在則需要刪除。執行腳本如下
rpm -qa mysql-client
測試中發現沒有安裝,不用刪除
l 檢查mysql- libs,如已存在則需要刪除。執行腳本如下
rpm -qa mysql-libs
mysql-libs-5.1.66-2.el6_3.x86_64
rpm -e mysql-libs --nodeps;
二、安裝mysql服務器
安裝mysql成功後,會為root隨機生成一個密碼,密碼保存在‘/root/.mysql_secret‘文件內。
[root@localhost mysql]# rpm -ivh MySQL-server-5.6.15-1.linux_glibc2.5.x86_64.rpm
Preparing... ########################################### [100%]
1:MySQL-server ########################################### [100%]
2016-05-12 15:37:48 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2016-05-12 15:37:48 22636 [Note] InnoDB: The InnoDB memory heap is disabled
2016-05-12 15:37:48 22636 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-05-12 15:37:48 22636 [Note] InnoDB: Compressed tables use zlib 1.2.3
2016-05-12 15:37:48 22636 [Note] InnoDB: Using Linux native AIO
2016-05-12 15:37:48 22636 [Note] InnoDB: Using CPU crc32 instructions
2016-05-12 15:37:48 22636 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2016-05-12 15:37:48 22636 [Note] InnoDB: Completed initialization of buffer pool
2016-05-12 15:37:48 22636 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2016-05-12 15:37:48 22636 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2016-05-12 15:37:48 22636 [Note] InnoDB: Database physically writes the file full: wait...
2016-05-12 15:37:48 22636 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2016-05-12 15:37:49 22636 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2016-05-12 15:37:50 22636 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2016-05-12 15:37:50 22636 [Warning] InnoDB: New log files created, LSN=45781
2016-05-12 15:37:50 22636 [Note] InnoDB: Doublewrite buffer not found: creating new
2016-05-12 15:37:50 22636 [Note] InnoDB: Doublewrite buffer created
2016-05-12 15:37:50 22636 [Note] InnoDB: 128 rollback segment(s) are active.
2016-05-12 15:37:50 22636 [Warning] InnoDB: Creating foreign key constraint system tables.
2016-05-12 15:37:50 22636 [Note] InnoDB: Foreign key constraint system tables created
2016-05-12 15:37:50 22636 [Note] InnoDB: Creating tablespace and datafile system tables.
2016-05-12 15:37:50 22636 [Note] InnoDB: Tablespace and datafile system tables created.
2016-05-12 15:37:50 22636 [Note] InnoDB: Waiting for purge to start
2016-05-12 15:37:50 22636 [Note] InnoDB: 5.6.15 started; log sequence number 0
A random root password has been set. You will find it in ‘/root/.mysql_secret‘.
2016-05-12 15:37:57 22636 [Note] Binlog end
2016-05-12 15:37:57 22636 [Note] InnoDB: FTS optimize thread exiting.
2016-05-12 15:37:57 22636 [Note] InnoDB: Starting shutdown...
2016-05-12 15:37:58 22636 [Note] InnoDB: Shutdown completed; log sequence number 1625977
2016-05-12 15:37:58 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2016-05-12 15:37:58 22659 [Note] InnoDB: The InnoDB memory heap is disabled
2016-05-12 15:37:58 22659 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-05-12 15:37:58 22659 [Note] InnoDB: Compressed tables use zlib 1.2.3
2016-05-12 15:37:58 22659 [Note] InnoDB: Using Linux native AIO
2016-05-12 15:37:58 22659 [Note] InnoDB: Using CPU crc32 instructions
2016-05-12 15:37:58 22659 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2016-05-12 15:37:58 22659 [Note] InnoDB: Completed initialization of buffer pool
2016-05-12 15:37:58 22659 [Note] InnoDB: Highest supported file format is Barracuda.
2016-05-12 15:37:58 22659 [Note] InnoDB: 128 rollback segment(s) are active.
2016-05-12 15:37:59 22659 [Note] InnoDB: Waiting for purge to start
2016-05-12 15:37:59 22659 [Note] InnoDB: 5.6.15 started; log sequence number 1625977
2016-05-12 15:37:59 22659 [Note] Binlog end
2016-05-12 15:37:59 22659 [Note] InnoDB: FTS optimize thread exiting.
2016-05-12 15:37:59 22659 [Note] InnoDB: Starting shutdown...
2016-05-12 15:38:01 22659 [Note] InnoDB: Shutdown completed; log sequence number 1625987
A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER !
You will find that password in ‘/root/.mysql_secret‘.
You must change that password on your first connect,
no other statement but ‘SET PASSWORD‘ will be accepted.
See the manual for the semantics of the ‘password expired‘ flag.
Also, the account for the anonymous user has been removed.
In addition, you can run:
/usr/bin/mysql_secure_installation
which will also give you the option of removing the test database.
This is strongly recommended for production servers.
See the manual for more instructions.
Please report any problems with the /usr/bin/mysqlbug script!
The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
New default config file was created as /usr/my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings
[root@localhost mysql]#
三、安裝mysql客戶端
[root@localhost mysql]# rpm -ivh MySQL-client-5.6.15-1.linux_glibc2.5.x86_64.rpm
Preparing... ########################################### [100%]
1:MySQL-client ########################################### [100%]
四、啟動mysqld服務
查找mysql.server
[root@localhost /]# find / -name mysql.server
/usr/share/mysql/mysql.server
拷貝mysql.server到/etc/rc.d/init.d/mysqld
[root@terminal opt]# cp /usr/share/mysql/mysql.server /etc/rc.d/init.d/mysqld
添加mysqld執行權限
[root@terminal opt]#chmod +x /etc/rc.d/init.d/mysqld
添加mysqld到系統服務
[root@terminal opt]#chkconfig --add mysqld
檢查mysqld是否生效
[root@terminal opt]# chkconfig --list mysqld
mysqld 0:關閉 1:關閉 2:啟用 3:啟用 4:啟用 5:啟用 6:關閉
五、啟動mysql和mysqld
[root@localhost mysql]# service mysql start
Starting MySQL. [確定]
[root@terminal opt]# service mysqld start
Starting MySQL. [確定]
六、關閉或重啟mysql和mysqld
mysql
[root@localhost ~]# service mysql restart
Shutting down MySQL.. [確定]
Starting MySQL. [確定]
[root@localhost ~]# service mysql stop
Mysqld服務
[root@terminal opt]# service mysqld restart
Shutting down MySQL.. [確定]
Starting MySQL. [確定]
[root@terminal opt]# service mysqld stop
七、修改root密碼、設置權限
使用客戶端連接mysql並修改root密碼,必須修改密碼,否則無法使用。
安裝mysql5.6成功後,會為root隨機生成一個密碼,我們首次使用root登陸時需要,需要使用這個密碼,然後修改root密碼。操作命令可以參考下文紅色字體。
[root@localhost mysql]# cat /root/.mysql_secret
# The random password set for the root user at Thu May 12 15:37:53 2016 (local time): nghlklAC
[root@localhost mysql]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.15
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
mysql> select 1;
ERROR 1820 (HY000): You must SET PASSWORD before executing this statement
mysql> set password=password(‘root‘);
Query OK, 0 rows affected (0.00 sec)
mysql> select 1;
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.00 sec)
設置權限。必須設置權限,否則mysql不允許本機外的其他客戶端連接。
mysql> GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘%‘ IDENTIFIED BY ‘root‘ WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)
附錄A、關閉SELinux權限的操作
設置權限。必須設置權限,否則mysql不允許本機外的其他客戶端連接。
查看SELinux權限,如果是Enforcing則說明沒有關閉權限。
[root@localhost mysql]#
Enforcing
修改/etc/selinux/config文件,將enforcing改為disabled
[root@localhost mysql]# vim /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
# targeted - Targeted processes are protected,
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
~
~
~
~
~
~
"/etc/selinux/config" 13L, 457C 已寫入
修改這個配置文件後,需要重啟linux服務器,才能生效。Disabled代表關閉了selinux。
Last login: Thu May 12 15:17:19 2016 from 192.168.2.130
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# getenforce
Disabled
附錄B、還原mysql數據庫
利用sql備份腳本
還原前,需要先創建數據庫,這樣做的目的是可以靈活定義數據庫名稱。
使用mysql客戶端,登錄到mysql(mysql -u root -p)。利用create命令創建需要的數據庫(例如create database db_name character set =utf8;),利用use命令將當前數據庫切換到剛剛創建的數據庫上(例如use db_name;),然後利用source命令還原數據庫(例如source /opt/xxx/20160513.sql)。其中,db_name為你需要用到的數據庫名稱。
[root@localhost ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 259
Server version: 5.6.15 MySQL Community Server (GPL)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
mysql> create database httc character set =utf8;
Query OK, 1 row affected (0.00 sec)
mysql> create database db_name character set =utf8;
Query OK, 1 row affected (0.00 sec)
mysql> use db_name;
Database changed
mysql> source /opt/xxx/20160513.sql
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.46 sec)
Query OK, 0 rows affected (0.00 sec)
。。。。。。
mysql>
附錄C、設置mysql不區分數據庫表名大小寫
查找my.cnf文件,然後在最後一行添加lower_case_table_names = 1,之後重啟mysql。
[root@localhost ~]# find / -name my.cnf
/usr/my.cnf
[root@localhost ~]# vim /usr/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
lower_case_table_names = 1
重啟後,在mysql客戶端執行查詢語句,出現如下結果則代表設置mysql不區分表名大小寫成功。
mysql> show variables like "%case%" ;
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| lower_case_file_system | OFF |
| lower_case_table_names | 1 |
+------------------------+-------+
2 rows in set (0.00 sec)
mysql>
2017-12-26--mysql(5.6.15),linux下安裝使用