Linux下安装mysql数据库(源码编译安装)
1. 下载mysql版本#wgethttp://ftp.ntu.edu.tw/pub/MySQL/Downloads/MySQL-5.5/mysql-5.5.32.tar.gz
2. 安装依赖以及编译工具cmake
#yum install –y cmake
#yum install –y ncurses-devel
3. 添加用户mysql
#useradd –u 8001 –s /sbin/nologin mysql
4. 创建数据库存放目录#mkdir /data
5. 编译安装mysql
#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 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/data-DMYSQL_USER=mysql
对应的参数说明:
-DCMAKE_INSTALL_PREFIX 数据文件存放目录
-DMYSQL_UNIX_ADDR sock文件路径
-DDEFAULT_CHARSET 默认字符集
-DDEFAULT_COLLATION 默认字符校对
-DWITH_EXTRA_CHARSETS 扩展字符支持 默认all
-DWITH_storage_STORAGE_ENGINE 存储引擎的支持,默认支持MyISAM,MERGE,MEMORY,CVS存储引擎
-DENABLED_LOCAL_INFILE=1 启用加载本地数据
-DMYSQL_DATADIR 数据存放目录
-DMYSQL_USER mysql运行用户
扩展:
-DWITH_PARTITION_STORAGE_ENGINE=1 支持分区表
-DINSTALL_LIBDIR=dir_name
-DSYSCONFDIR=dir_name The defaultmy.cnf option file directory
6. 安装#make&&makeinstall
7. 对mysql用户授权
#chown-R mysql:mysql /usr/local/mysql/
#chown–R mysql:mysql /data
#mkdir /var/mysql
#chmod 755 /var/mysql
8. 创建配置文件#cpsupport-files/my-large.cnf /etc/my.cnf
9. 设置环境变量
#echo ‘export PATH=/usr/local/mysql/bin:’>>/etc/profile
#source !$
10. 创建服务启动脚本,添加至开机启动
#cp support-files/mysql.server/etc/init.d/mysqld
#chmod +x /etc/init.d/mysqld 给脚本添加执行权限
#vim /etc/init.d/mysqld 服务启动脚本要修改以下两个参数
basedir=/usr/local/mysql MySQL安装目录
datadir= /data 数据存放目录
#chkconfig mysqld on 添加开机启动
11. 初始化数据库
#chmod +xscripts/mysql_install_db
#/usr/local/mysql/scripts/mysql_install_db--defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/data--user=mysql
12. Mysql初始化安全配置
#mysql_secure_installation
13. 安装过程中遇到的错误
-bash: mysql: commandnot found
原因:
是因为mysql命令的路径在/usr/local/mysql/bin下面,所以你直接使用mysql命令时,系统在/usr/bin下面查此命令,所以找不到了。
解决办法:
ln -s/usr/local/mysql/bin/mysql /usr/bin
※ CouldNOT find Curses (missing: CURSES_LIBRARYCURSES_INCLUDE_PATH)
错误重现:
该错误会在安装cmake时发生,原因是以为没有执行第一步。当你执行完第一步重做cmake前需要先rm CMakeCache.txt。
-- Could NOT findCurses (missing: CURSES_LIBRARYCURSES_INCLUDE_PATH)
CMake Error atcmake/readline.cmake:82 (MESSAGE):
Curses library not found. Please install appropriate package,
remove CMakeCache.txt and rerun cmake.OnDebian/Ubuntu, package name is libncurses5-dev, on Redhat and derivates it isncurses-devel.
Call Stack (most recentcall first):
cmake/readline.cmake:126 (FIND_CURSES)
cmake/readline.cmake:216 (MYSQL_USE_BUNDLED_LIBEDIT)
CMakeLists.txt:250 (MYSQL_CHECK_READLINE)
出现这种错误
Enter current password for root (enter fornone):
ERROR 1045 (28000): Access denied for user'root'@'localhost' (using password: YES)
干掉mysql进程
pkill mysqld
rm -rf /data/*
重新初始化
14. Mysql安全优化
把用户都删了,添加一个额外的管理员
mysql> delete from mysql.user;
mysql> grant all privileges on *.* tosystem@'localhost' identified by '123456' with grant option;
mysql> flush privileges;
mysql>drop test database;