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>