Linux ( CentOS ) MySQL 安装
MySQL 是最流行的关系型数据库管理系统,由瑞典 MySQL AB 公司开发,目前属于 Oracle 公司
MySQL 所使用的 SQL 语言是用于访问数据库的最常用标准化语言
MySQL 由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,一般中小型网站的开发都选择MySQL作为网站数据库
MySQL 安装
本教程的系统平台:CentOS release 7.x (Final) 64位。
一、安装编译工具及库文件
[root@localhost ~ ]# yum -y install gcc gcc-c++ make autoconf libtool-ltdl-devel gd-devel freetype-devel libxml2-devel libjpeg-devel libpng-devel openssl-devel curl-devel bison patch unzip libmcrypt-devel libmhash-devel ncurses-devel sudo bzip2 flex libaio-devel
二、 安装cmake 编译器
cmake 版本:cmake-3.1.1
1、下载地址: http://www.cmake.org/files/v3.1/cmake-3.1.1.tar.gz
[root@localhost ~ ]# wget http://www.cmake.org/files/v3.1/cmake-3.1.1.tar.gz
2、解压安装包
[root@localhost ~ ]# tar zxvf cmake-3.1.1.tar.gz
3、进入安装包目录
[root@localhost ~ ]# cd cmake-3.1.1
4、编译安装
$ ./bootstrap
$ make && make install
三、安装 MySQL
MySQL版本:mysql-5.7.10
1、下载地址: http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.10.tar.gz
[root@localhost ~ ]# wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.10.tar.gz
输出
[root@localhost ~ ]# wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.10.tar.gz --2017-09-15 17:00:21-- http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.10.tar.gz Resolving dev.mysql.com... 137.254.60.11 Connecting to dev.mysql.com|137.254.60.11|:80... connected. HTTP request sent, awaiting response... 301 Moved Permanently Location: https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.10.tar.gz [following] --2017-09-15 17:00:22-- https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.10.tar.gz Connecting to dev.mysql.com|137.254.60.11|:443... connected. HTTP request sent, awaiting response... 302 Found Location: https://cdn.mysql.com//archives/mysql-5.7/mysql-5.7.10.tar.gz [following] --2017-09-15 17:00:23-- https://cdn.mysql.com//archives/mysql-5.7/mysql-5.7.10.tar.gz Resolving cdn.mysql.com... 23.8.231.210 Connecting to cdn.mysql.com|23.8.231.210|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 48919371 (47M) [application/x-tar-gz] Saving to: 'mysql-5.7.10.tar.gz' mysql-5.7.10.tar.gz 13%[====> ] 6.07M 231KB/s eta 3m 1s
2、解压安装包
[root@localhost ~ ]# tar zxvf mysql-5.7.10.tar.gz
3、进入安装包目录
[root@localhost ~ ]# cd mysql-5.7.10
4、编译安装
[root@localhost ~ ]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql/ -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DWITH_INNODB_MEMCACHED=1 -DWITH_DEBUG=OFF -DWITH_ZLIB=bundled -DENABLED_LOCAL_INFILE=1 -DENABLED_PROFILING=ON -DMYSQL_MAINTAINER_MODE=OFF -DMYSQL_DATADIR=/usr/local/mysql/data -DMYSQL_TCP_PORT=3306
[root@localhost ~ ]# make && make install
5、查看 mysql 版本:
[root@localhost ~ ]# mysql --version
输出:
mysql Ver 14.14 Distrib 5.7.10, for redhat-linux-gnu (x86_64) using readline 5.1
到此,mysql安装完成。
四、 MySQL 配置
1、创建 mysql 运行使用的用户 mysql
和 组 mysql
:
[root@localhost ~ ]# groupadd mysql [root@localhost ~ ]# useradd -g mysql mysql
2、创建 binlog 和库的存储路径并赋予 mysql 用户权限
[root@localhost ~ ]# mkdir -p /usr/local/mysql/binlog /mnt/data/data_mysql [root@localhost ~ ]# mkdir -p /mnt/data/wwwlogs/mysql # 创建日志文件目录 [root@localhost ~ ]# mkdir -p /var/run/mysql #创建 pid 文件目录 [root@localhost ~ ]# chown mysql.mysql /usr/local/mysql/binlog/ [root@localhost ~ ]# chown mysql.mysql /mnt/data/data_mysql/ [root@localhost ~ ]# chown mysql.mysql /mnt/data/wwwlogs/mysql /var/run/mysql
3、创建 /etc/my.cnf
配置文件
[root@localhost ~ ]# touch /etc/my.cnf
将 /etc/my.cnf
替换为下面内容
[client] port = 3306 socket = /tmp/mysql.sock [mysqld] replicate-ignore-db = mysql replicate-ignore-db = test replicate-ignore-db = information_schema user = mysql port = 3306 socket = /tmp/mysql.sock basedir = /usr/local/mysql datadir = /mnt/data/data_mysql log-error = /mnt/data/wwwlogs/mysql/mysql_error.log pid-file = /var/run/mysql/mysql.pid open_files_limit = 65535 back_log = 600 max_connections = 5000 max_connect_errors = 1000 table_open_cache = 1024 external-locking = FALSE max_allowed_packet = 32M sort_buffer_size = 1M join_buffer_size = 1M thread_cache_size = 600 #thread_concurrency = 8 query_cache_size = 128M query_cache_limit = 2M query_cache_min_res_unit = 2k default-storage-engine = MyISAM default-tmp-storage-engine=MYISAM thread_stack = 192K transaction_isolation = READ-COMMITTED tmp_table_size = 128M max_heap_table_size = 128M log-slave-updates log-bin = /usr/local/mysql/binlog/binlog binlog-do-db=oa_fb binlog-ignore-db=mysql binlog_cache_size = 4M binlog_format = MIXED max_binlog_cache_size = 8M max_binlog_size = 1G relay-log-index = /usr/local/mysql/relaylog/relaylog relay-log-info-file = /usr/local/mysql/relaylog/relaylog relay-log = /usr/local/mysql/relaylog/relaylog expire_logs_days = 10 key_buffer_size = 256M read_buffer_size = 1M read_rnd_buffer_size = 16M bulk_insert_buffer_size = 64M myisam_sort_buffer_size = 128M myisam_max_sort_file_size = 10G myisam_repair_threads = 1 myisam_recover interactive_timeout = 120 wait_timeout = 120 skip-name-resolve #master-connect-retry = 10 slave-skip-errors = 1032,1062,126,1114,1146,1048,1396 #master-host = 192.168.1.2 #master-user = username #master-password = password #master-port = 3306 server-id = 1 loose-innodb-trx=0 loose-innodb-locks=0 loose-innodb-lock-waits=0 loose-innodb-cmp=0 loose-innodb-cmp-per-index=0 loose-innodb-cmp-per-index-reset=0 loose-innodb-cmp-reset=0 loose-innodb-cmpmem=0 loose-innodb-cmpmem-reset=0 loose-innodb-buffer-page=0 loose-innodb-buffer-page-lru=0 loose-innodb-buffer-pool-stats=0 loose-innodb-metrics=0 loose-innodb-ft-default-stopword=0 loose-innodb-ft-inserted=0 loose-innodb-ft-deleted=0 loose-innodb-ft-being-deleted=0 loose-innodb-ft-config=0 loose-innodb-ft-index-cache=0 loose-innodb-ft-index-table=0 loose-innodb-sys-tables=0 loose-innodb-sys-tablestats=0 loose-innodb-sys-indexes=0 loose-innodb-sys-columns=0 loose-innodb-sys-fields=0 loose-innodb-sys-foreign=0 loose-innodb-sys-foreign-cols=0 slow_query_log_file=/mnt/data/wwwlogs/mysql/mysql_slow.log long_query_time = 1 [mysqldump] quick max_allowed_packet = 32M
4、初始化数据库
[root@localhost ~ ]# /usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --user=mysql
显示如下信息:
Installing MySQL system tables...2017-09-13 14:23:32 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). OK Filling help tables...2017-09-13 14:23:45 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). OK ...
5、创建开机启动脚本
[root@localhost ~]# cd /usr/local/mysql/ [root@localhost mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld [root@localhost mysql]# chkconfig --add mysqld [root@localhost mysql]# chkconfig --level 345 mysqld on
6、启动 mysql 服务器
[root@localhost ~]# service mysqld start Starting MySQL.......... SUCCESS!
7、连接 MySQL
[root@localhost ~]# mysql -u root -p Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.10 Homebrew Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]>
修改MySQL用户密码
命令行输入
[root@localhost ~]# mysqladmin -u[用户名] -p[旧密码] password [新密码]
范例: 修改 demo@127.0.0.1 的密码为 123456
[root@localhost ~]# mysqladmin -udemo -p123456 password 123456
如果已经登录进入 mysql 命令行
SET PASSWORD FOR '用户名'@'主机' = PASSWORD('密码');
范例: 修改 demo@127.0.0.1 的密码为 123456
SET PASSWORD FOR 'demo'@'127.0.0.1' = PASSWORD('123456');
创建新用户并授权
grant all privileges on *.* to '用户名'@'%' identified by '密码' with grant option;
范例: 创建 demo@localhost 用户,密码设置为 123456
,并赋予所有权限
grant all privileges on *.* to 'demo'@'localhost' identified by '123456' with grant option;
其它 MySQL 常用命令
1、 启动 MySQL
[root@localhost ~]# service mysqld start
2、 停止 MySQL
[root@localhost ~]# service mysqld stop
3、 重启 MySQL
[root@localhost ~]# service mysqld restart
4、 重新加载 MySQL 配置
[root@localhost ~]# service mysqld reload