天天看點

mysql 初始化安裝程式_MySQL的安裝、配置檔案和初始化

STEP05 前面的步驟我們都是使用root或其他使用者執行的指令,下面我們把MySQL的路徑權限配置設定給mysql.mysql: [[email protected] local]# pwd

/usr/local

[[email protected] local]# chown -R mysql:mysql /usr/local/mysql-5.1.73/執行指令之後,我們再看下/usr/local/mysql-5.1.73的目錄結構:

mysql 初始化安裝程式_MySQL的安裝、配置檔案和初始化

STEP06 下面我們建立指向真實版本的軟連接配接:[[email protected] local]# ln -s /usr/local/mysql-5.1.73/ /usr/local/mysql

[[email protected] local]# chown -R mysql.mysql /usr/local/mysql執行指令之後,我們有如下的目錄結構:

mysql 初始化安裝程式_MySQL的安裝、配置檔案和初始化

MySQL的配置檔案 進行完上述步驟之後,MySQL已然安裝成功。下面就可以初始化和啟動了。但在初始化之前,我們首先要了解下my.cnf這個配置檔案,因為它與後續的初始化和運作有着莫大的關系。當mysqld服務啟動時,系統預設按一定的順序讀取配置檔案。該搜尋順序,我們可以使用如下的指令得到。 [[email protected] libexec]# ./mysqld --verbose --help | more

./mysqld Ver 5.1.73 for unknown-linux-gnu on x86_64 (Source distribution)

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.

Starts the MySQL database server.

Usage: ./mysqld [OPTIONS]

Default options are read from the following files in the given order:

/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql-5.1.73/etc/my.cnf ~/.my.cnf紅色文字就是搜尋順序,系統預設是按/etc/my.cnf,/etc/mysql/my.cnf,/usr/local/mysql-5.1.73/etc/my.cnf,~/.my.cnf的順序讀取配置檔案(其中/usr/local/mysql-5.1.73為安裝目錄),當有多個配置檔案時,mysql會以讀取到的第一個配置檔案中的參數為準。

MySQL的初始化 MySQL安裝後,還不能直接啟動服務,必須先對資料庫進行初始化。初始化的工作主要包括:

** 初始化日志、表空間等資料庫必須的檔案;

** 建立并初始化系統資料庫。

在之行configure腳本配置編譯參數時,我們曾經指定“--datadir=/usr/local/mysql-5.1.73/data”用來表明資料庫檔案的存放路徑。打開bin/mysql_install_db腳本檔案,會發現編譯程式會把該路徑寫入該檔案,用作資料庫的預設初始化路徑。是以我們可以不用加其他參數,使用如下指令直接初始化。[[email protected] mysql-5.1.73]# bin/mysql_install_db --user=mysql

Installing MySQL system tables...

OK

Filling help tables...

OK

To start mysqld at boot time you have to copy

support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !

To do so, start the server, then issue the following commands:

/usr/local/mysql-5.1.73/bin/mysqladmin -u root password 'new-password'

/usr/local/mysql-5.1.73/bin/mysqladmin -u root -h localhost.localdomain password 'new-password'

Alternatively you can run:

/usr/local/mysql-5.1.73/bin/mysql_secure_installation

which will also give you the option of removing the test

databases and anonymous user created by default. This is

strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:

cd /usr/local/mysql-5.1.73 ; /usr/local/mysql-5.1.73/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl

cd /usr/local/mysql-5.1.73/mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/local/mysql-5.1.73/bin/mysqlbug script!

上述的列印資訊給了我們一個重要的提醒,就是不要忘記給root設定一個密碼,防止惡意使用者獲得資料庫最高權限,或者被其他合法使用者誤使用root權限而造成事故。

MySQL的管理:啟動和停止 初始化資料庫之後,我們就可以啟動mysqld守護程序,開始通路資料庫了。MySQL的啟停大緻有如下這麼幾種方法,在實際環境中,我們根據使用場景來選取最合适的方法:

$mysql_dir/bin/mysqladmin -u root -p shutdown

$mysql_dir/bin/mysqld_safe &

[[email protected] mysql-5.1.73]# bin/mysqld_safe --user=mysql &

[1] 15275

[[email protected] mysql-5.1.73]# 150513 00:22:09 mysqld_safe Logging to '/var/log/mysqld.log'.

150513 00:22:09 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

150513 00:22:10 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended

[[email protected] lib]# ps aux | grep mysql

root 16035 0.0 0.1 106100 1356 pts/0 S+ 00:36 0:00 /bin/sh bin/mysqld_safe

mysql 16116 0.0 2.3 351888 24288 pts/0 Sl+ 00:36 0:00 /usr/local/mysql-5.1.73/libexec/mysqld --basedir=/usr/local/mysql-5.1.73 --datadir=/usr/local/mysql-5.1.73/data --user=mysql --log-error=/usr/local/mysql-5.1.73/mysqld.log --pid-file=/usr/local/mysql-5.1.73/mysqld.pid --socket=/usr/local/mysql-5.1.73/mysql.sock

root 16185 0.0 0.0 103252 820 pts/2 S+ 00:41 0:00 grep mysqlMySQL的測試程式 #include

#include

#include "mysql.h"

using namespace std;

const string DB_CONFIG_HOST = "127.0.0.1";

const string DB_CONFIG_USER = "easenote";

const string DB_CONFIG_PASSWD = "[email protected]";

const string DB_CONFIG_DBNAME = "mysql";

const int DB_CONFIG_PORT = 3306;

int main() {

//STEP01 連接配接資料庫

MYSQL * mHandle = mysql_init(NULL);

if (mysql_real_connect(mHandle,

DB_CONFIG_HOST.c_str(),

DB_CONFIG_USER.c_str(),

DB_CONFIG_PASSWD.c_str(),

DB_CONFIG_DBNAME.c_str(),

DB_CONFIG_PORT, NULL, 0) == NULL)

{

cout << "mysql_real_connect fault: " << mysql_errno(mHandle) << "," << string(mysql_error(mHandle)) << endl;

return -1;

}

//STEP02 從mysql.test讀取資料

string sCommand = "SELECT Host,User FROM mysql.user LIMIT 1";

if (mysql_real_query(mHandle, sCommand.c_str(), sCommand.length()) != 0)

{

cout << "mysql_real_query fault: " << mysql_errno(mHandle) << "," << string(mysql_error(mHandle)) << endl;

return -1;

}

MYSQL_RES *pstRes = mysql_store_result(mHandle);

if (pstRes == NULL)

{

cout << "mysql_store_result fault: " << mysql_errno(mHandle) << "," << string(mysql_error(mHandle)) << endl;

return -1;

}

//STEP03 從結果集中讀出資料

MYSQL_ROW stRow;

while((stRow = mysql_fetch_row(pstRes)) != (MYSQL_ROW)NULL)

{

unsigned long * arrLens = mysql_fetch_lengths(pstRes);

cout << "HOST:" << string(stRow[0], arrLens[0]) << endl;

cout << "USER:" << string(stRow[1], arrLens[1]) << endl;

}

mysql_free_result(pstRes);

mysql_close(mHandle);

return 0;

}現在我們再寫一個Makefile檔案來編譯上述代碼: MYSQL_INC := /usr/local/mysql/include/mysql

MYSQL_LIB := /usr/local/mysql/lib/mysql

all:main.cpp

g++ -o main main.cpp -I${MYSQL_INC} -L${MYSQL_LIB} -lmysqlclient

clean:

-rm -f mainMySQL的管理:啟動多個執行個體MySQL的管理:授權MySQL開發環境測試