STEP01 创建MySQL运行期用户和用户组,当前步骤也可以在安装成功之后做:
[root@localhost objs]# groupadd mysql [root@localhost objs]# useradd -g mysql mysqlSTEP02 将下载好的代码包拷贝到目标服务器,然后解压:
[root@localhost objs]# pwd /home/motadou/objs [root@localhost objs]# tar -zxvf mysql-5.1.73.tar.gz
[root@localhost mysql-5.1.73]# pwd /home/motadou/objs/mysql-5.1.73 [root@localhost mysql-5.1.73]# ./configure --prefix=/usr/local/mysql-5.1.73 --datadir=/usr/local/mysql-5.1.73/data --without-debug --enable-thread-safe-client --with-charset=utf8 --with-extra-charsets=latin1,gbk --with-mysqld-user=mysql --with-unix-socket-path=/usr/local/mysql-5.1.73/sock/mysql.sock --with-plugins=innobase,partitionSTEP04 配置相应参数之后,如果顺利,将在当前目录下生成好Makfile文件,然后我们执行编译命令:
[root@localhost mysql-5.1.73]# pwd /home/motadou/objs/mysql-5.1.73 [root@localhost mysql-5.1.73]# make ...... make[1]: Entering directory `/home/motadou/objs/mysql-5.1.73/win' make[1]: Nothing to be done for `all'. make[1]: Leaving directory `/home/motadou/objs/mysql-5.1.73/win' [root@localhost mysql-5.1.73]#如果顺利,我们会看到如上的编译成功信息。进行到此,MySQL库以及可执行程序已经编译出来,可以说已经无限接近成功。现在我们将其安装到指定的目录:
[root@localhost mysql-5.1.73]# pwd /home/motadou/objs/mysql-5.1.73 [root@localhost mysql-5.1.73]# make install ...... make[1]: Entering directory `/home/motadou/objs/mysql-5.1.73/win' make[2]: Entering directory `/home/motadou/objs/mysql-5.1.73/win' make[2]: Nothing to be done for `install-exec-am'. make[2]: Nothing to be done for `install-data-am'. make[2]: Leaving directory `/home/motadou/objs/mysql-5.1.73/win' make[1]: Leaving directory `/home/motadou/objs/mysql-5.1.73/win'安装成功之后,我们看下/usr/local/mysql-5.1.73的目录结构:
[root@localhost local]# pwd /usr/local [root@localhost local]# chown -R mysql:mysql /usr/local/mysql-5.1.73/执行命令之后,我们再看下/usr/local/mysql-5.1.73的目录结构:
[root@localhost local]# ln -s /usr/local/mysql-5.1.73/ /usr/local/mysql [root@localhost local]# chown -R mysql.mysql /usr/local/mysql执行命令之后,我们有如下的目录结构:
[root@localhost 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会以读取到的第一个配置文件中的参数为准。 ** 初始化日志、表空间等数据库必须的文件;
** 创建并初始化系统数据库。
[root@localhost 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_dir/bin/mysqladmin -u root -p shutdown $mysql_dir/bin/mysqld_safe &
[root@localhost mysql-5.1.73]# bin/mysqld_safe --user=mysql & [1] 15275 [root@localhost 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 [root@localhost 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 mysql
#include <string> #include <iostream> #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 = "easenote@123456"; 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开发环境测试
** http://www.cnblogs.com/xiongpq/p/3384681.html
** http://linux.it.net.cn/e/data/mysql/2015/0222/13227.html
** http://caisangzi.blog.51cto.com/6387416/1296931