MySQL是一款开源的单进程多线程的关系型数据库,其为C/S架构;其可靠性高、伸缩性强,易用。且支持C、C++、Python、PHP等开发
其版本有a版(内测版)、B版(公测版本)、RC(Release Candidate)发行候选版本、及GA(General Availablity)正式发布版本
且分别为社区版和商业版
我在这里安装的是社区版的GA版本 mysql-5.6.34
MySQL的安装:
专用软件包安装RPM(Rhel SUSE CentoS)、Deb
通用二进制格式包gcc: X86、X_64
源码编译:在MySQL5.5后要用cmake编译
如果mysql和mysqld在同一台unix系统上,则进程间通信为基于mysql.sock
如果mysql和mysqld在同一台windows系统上,则进程间通信为基于memory(共享内存或者pipe管道)
如果mysql和mysqld不在同一台主机上,则进程间通信为基于TCP/IP,其效率不如前两者
MySQL客户端工具:
mysql
mysqldump
mysqladmin
mysqlcheck
mysqlimport
一、安装cmake
跨平台编译器,可以对一个源码树编译为不同的版本,如编译一个32位的和一个64位的且存放在不同目录下
# tar xf cmake-2.8.8.tar.gz
# cd cmake-2.8.8
# ./configure
# make
# make install
二、编译安装mysql-5.5.25a
1、使用cmake编译mysql-5.5
cmake指定编译选项的方式不同于make,其实现方式对比如下:
./configure cmake .
./configure --help cmake . -LH or ccmake .
指定安装文件的安装路径时常用的选项:
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql
-DMYSQL_DATADIR=/data/mysql
-DSYSCONFDIR=/etc
默认编译的存储引擎包括:csv、myisam、myisammrg和heap。若要安装其它存储引擎,可以使用类似如下编译选项:
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_ARCHIVE_STORAGE_ENGINE=1
-DWITH_BLACKHOLE_STORAGE_ENGINE=1
-DWITH_FEDERATED_STORAGE_ENGINE=1
若要明确指定不编译某存储引擎,可以使用类似如下的选项:
-DWITHOUT_<ENGINE>_STORAGE_ENGINE=1
比如:
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1
-DWITHOUT_FEDERATED_STORAGE_ENGINE=1
-DWITHOUT_PARTITION_STORAGE_ENGINE=1
如若要编译进其它功能,如SSL等,则可使用类似如下选项来实现编译时使用某库或不使用某库:
-DWITH_READLINE=1 (能使用load infile这种方式批量导入MySQL数据)
-DWITH_SSL=system (支持ssl功能)
-DWITH_ZLIB=system (支持zlib压缩库)
-DWITH_LIBWRAP=0 (是否支持tcpwrap实现控制)
其它常用的选项:
-DMYSQL_TCP_PORT=3306
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock
-DENABLED_LOCAL_INFILE=1
-DEXTRA_CHARSETS=all
-DDEFAULT_CHARSET=utf8
-DDEFAULT_COLLATION=utf8_general_ci
-DWITH_DEBUG=0
-DENABLE_PROFILING=1
如果想清理此前的编译所生成的文件,则需要使用如下命令:
make clean
rm CMakeCache.txt
2、提前创建一个LVM分区,和MySQL用户、MySQL组,并将LV格式化后挂载到/mydata目录下,在进行MySQL初始化时指定数据存储目录为/mydata/data
3、编译安装
[root@node1 ~]# groupadd -r mysql ## -r创建系统用户
[root@node1 ~]# useradd -r mysql -g mysql -s /sbin/nologin
[roqot@node1 ~]# mkdir /mydata/data -p
[root@node1 ~]# chown -R mysql:mysql /mydata/data ##将存储目录改为mysql用户mysql组
[root@node1 ~]# cd /tool
[root@node1 tool]# tar -xf mysql-5.6.34.tar.gz
[root@node1 tool]# cd mysql-5.6.34
[root@node1 mysql-5.6.34]#
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DSYSCONFDIR=/etc \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci
[root@node1 mysql-5.6.34]# make && make install
[root@node1 mysql]# chown -R mysql:mysql ./*
[root@node1 mysql]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data ##初始化
[root@node1 mysql]# chown -R root /usr/local/mysql/*
[root@node1 mysql]# cp support-files/mysql.server /etc/init.d/mysqld #复制样例配置文件
[root@node1 mysql]# cp support-files/mysql.server /etc/init.d/mysqld #加入init脚本
[root@node1 mysql]# chkconfig --add mysql ##加入系统服务
[root@node1 mysql]# service mysqld start
[root@node1 mysql]# vim /etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin
[root@node1 mysql]# . /etc/profile.d/mysql.sh ##source一下这个文件
[root@node1 mysql]# echo $PATH
看能不能连接上去
[root@node1 mysql]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.34-log Source distribution
Copyright (c) 2000, 2016, 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
mysql> use mysql
mysql> select user,host,password from user;
+------+---------------+-------------------------------------------+
| user | host | password |
+------+---------------+-------------------------------------------+
| root | localhost | |
| root | node1.zxl.com | |
| root | 127.0.0.1 | |
| root | ::1 | |
| | node1.zxl.com | |
| root | 192.168.%.% | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+------+---------------+-------------------------------------------+
mysql> drop user ''@localhost; #删除匿名用户
Query OK, 0 rows affected (0.15 sec)
mysql> drop user ''@node1.zxl.com; #删除匿名用户
Query OK, 0 rows affected (0.00 sec)
mysql> drop user 'root'@'::1'; #删除ipv6的用户
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host,password from user;
+------+---------------+-------------------------------------------+
| user | host | password |
+------+---------------+-------------------------------------------+
| root | localhost | |
| root | node1.zxl.com | |
| root | 127.0.0.1 | |
| root | 192.168.%.% | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+------+---------------+-------------------------------------------+
4 rows in set (0.00 sec)
#为剩下的所有root用户改密码为‘123’
mysql> update user set password=password('123') where user='root';
Query OK, 4 rows affected (0.03 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> select user,host,password from user;
+------+---------------+-------------------------------------------+
| user | host | password |
+------+---------------+-------------------------------------------+
| root | localhost | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | node1.zxl.com | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | 127.0.0.1 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | 192.168.%.% | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+------+---------------+-------------------------------------------+
4 rows in set (0.00 sec)
mysql> flush privileges; #刷新生效
Query OK, 0 rows affected (0.00 sec)
mysql> \q
Bye
[root@node1 mysql]# mysql #退出重新连、连不上去
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@node1 mysql]# mysql -uroot -p #指定用户和密码
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.34-log Source distribution
Copyright (c) 2000, 2016, 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>\q
让MySQL支持远程连接
[root@node1 mysql]# cd
[root@node1 ~]# vim .my.cnf #在家目录下创建一个隐藏目录
[client]
user='root'
password='123'
host='localhost'
[root@node1 ~]# mysql #这样又可以直接连接上去了
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.6.34-log Source distribution
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| api |
| archiver |
| config |
| data |
| install |
| mysql |
| performance_schema |
| source |
| static |
| template |
| test |
| uc_client |
| uc_server |
| ultrax |
+--------------------+
15 rows in set (0.31 sec)
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec) ##默认引擎为innodb
mysql> \q
[root@node1 mysql]# vim /etc/my.cnf
[mysqld]
datadir=/mydata/data ##数据存储目录
innodb_file_per_table = ON
log-bin = master-bin
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[root@node1 mysql]# cd /mydata/data/mysql/
[root@node1 mysql]# ls
db.frm 表的结构定义文件
db.MYD 表的数据文件
db.MYI 表的索引文件
对myisam引擎来说:每个表有三个文件 .frm(表结构).MYD(表数据) .MYI(表索引)
对innodb引擎来说:所有的表共享一个表空间,但是这样不支持许多高级特性,建议每表使用一个独立的表空间
那么innodb如何使用每表独立空间?
mysql> show variables like '%innodb%'; #显示关于innodb的所有变量
| innodb_file_per_table | off |
mysql> \q
[root@node1 mysql]# vim /etc/my.cnf
innodb_file_per_table = 1
[root@node1 mysql]# service mysqld restart
[root@node1 mysql]#mysql
mysql> show variables like '%innodb%';
| innodb_file_per_table | ON |
创建一个数据库mydb,一个表testdb
mysql>
mysql> create database mydb;
Query OK, 1 row affected (0.43 sec)
mysql> use mydb
Database changed
mysql> create table testdb(id INT NOT NULL, name char(30));
Query OK, 0 rows affected (0.44 sec)
mysql> \q
[root@node1 mysql]# cd /mydata/data/mydb/
[root@node1 mydb]# ls
db.opt #这个数据库的默认排序规则和字符集(几乎每个数据库都会生成)
testdb.frm #表结构
testdb.ibd #每表一个表空间
innodb采用每表一个表空间后:.frm(表结构).ibd 表空间(表数据和表索引)
要不然所有表都使用一个表空间