CenterOS6.5(Minimal) 搭建MySQL

安装基础库请参考这里

MySQL

下载安装

  1. wget http://cdn.mysql.com/Downloads/MySQL-5.5/mysql-5.6.19.tar.gz -P /usr/local/app/sources/
  2. cd /usr/local/app/sources/
  3. tar -xf mysql-5.6.19.tar.gz
  4. cd mysql-5.6.19
  5. cmake -DCMAKE_INSTALL_PREFIX=/usr/local/app/mysql
  6. make
  7. make install clean

配置修改

  1. 创建mysql用户和组 groupadd mysql, useradd -g mysql mysql

  2. 修改/usr/local/app/mysql的用户和组 chown -R mysql:mysql /usr/local/app/mysql

  3. cp /usr/local/app/mysql/support-files/my-default.cnf /usr/local/app/mysql/my.cnf

  4. my.cnf中增加如下内容

[mysqld]
basedir = /usr/local/app/mysql
datadir = /usr/local/app/mysql/data
port = 9001
server_id = 1
socket = /usr/local/app/mysql/data/mysqld.sock
pid-file = /usr/local/app/mysql/data/mysqld.pid

character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'

innodb_buffer_pool_size = 32M
innodb_log_group_home_dir = /usr/local/app/mysql/data

join_buffer_size = 8M
sort_buffer_size = 8M
read_rnd_buffer_size = 8M
general_log = 1
general_log_file = /usr/local/app/mysql/data/mysqld.log

log_bin = /usr/local/app/mysql/data/mysql-bin
log_error = /usr/local/app/mysql/data/mysqld-error.log
max_connections = 1000
long_query_time = 2
slow_query_log = 1
slow_query_log_file = /usr/local/app/mysql/data/mysqld-slow.log

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

[client]
default-character-set=utf8mb4
[mysql]
default-character-set = utf8mb4
  1. /usr/local/app/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/app/mysql --datadir=/usr/local/app/mysql/data --explicit_defaults_for_timestamp

  2. 启动MySQL /usr/local/app/mysql/bin/mysqld_safe --defaults-file=/usr/local/app/mysql/my.cnf --user=mysql

  3. 连接mysql 配置用户密码等 /usr/local/app/mysql/bin/mysql -uroot -h127.0.0.1

  4. 创建一个数据库 create database dbname;

  5. grant all privileges on dbname.* to mysqluser@’127.0.0.1’ identified by ‘mysqluserpasswd’;

  6. commit;

  7. flush privileges;

  8. 修改mysql管理帐户的密码(待补充)

测试

  1. 上个部分创建的数据库中创建一个表user并向其中插入几条数据

  2. 修改/home/www/app/index.php 为如下内容

$connect = mysqli_connect('127.0.0.1','mysqluser','123456', 'dbname') or die('Unale to connect');
$sql = "select * from user";
$result = mysqli_query($connect,$sql);
$response['index'] = array();
while($row = mysqli_fetch_assoc($result)){
    array_push($response['index'], $row);
}
echo json_encode($response);
  1. 在浏览器中查看结果

你可能感兴趣的:(Linux)