Linux下MySQL5.6源码安装

时间:2023-01-27 17:12:25

1、首先安装依赖包,避免在安装过程中出现问题

shell>yum install cmake                --(mysql5.5以后是通过cmake来编译的)
shell>yum install ncurses-devel --Redhat执行cmake是需要依赖的包,如缺少编译报错
shell>yum install bison-devel
shell>yum install libaio-devel
shell>yum install gcc-c++

2、创建mysql用户及用户组

shell> groupadd mysql
shell> useradd -r -g mysql mysql // -r option means mysql is only a system account and can't login as a regular user
shell> id mysql
uid=497(mysql) gid=502(mysql) 组=502(mysql)

3、创建mysql的安装目录及数据库存放目录

shell> mkdir -p /db/mysql                 #安装mysql 
shell> mkdir -p /db/mysql/data #存放数据库

4、安装mysql

shell>  unzip mysql-5.6.19.zip
shell> cd mysql-5.6.19
shell> cmake . -DCMAKE_INSTALL_PREFIX=/db/mysql
-DMYSQL_DATADIR=/db/mysql/data
-DDEFAULT_CHARSET=utf8
-DDEFAULT_COLLATION=utf8_general_ci
shell> make && make install

CMAKE参数说明:

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql //默认安装目录
-DINSTALL_DATADIR=/usr/local/mysql/data //数据库存放目录
-DDEFAULT_CHARSET=utf8     //使用utf8字符
-DDEFAULT_COLLATION=utf8_general_ci //校验字符
-DEXTRA_CHARSETS=all   //安装所有扩展字符集
-DENABLED_LOCAL_INFILE=1   //允许从本地导入数据
-DMYSQL_USER=mysql
-DMYSQL_TCP_PORT=3306

CMAKE详细介绍参见

注意事项:
如安装失败重新编译时,需要清除旧的对象文件和缓存信息。
shell> make clean
shell> rm -f CMakeCache.txt
shell> rm -rf /etc/my.cnf

5、设置目录权限并创建系统数据库的表

shell> chown -R mysql.mysql /db/mysql
shell> cp /db/mysql/support-files/my-default.cnf /etc/my.cnf
shell> cp /db/mysql/support-files/mysql.server /etc/rc.d/init.d/mysql //将mysql的启动服务添加到系统服务中
shell> /db/mysql/scripts/mysql_install_db --basedir=/db/mysql/ --datadir=/db/mysql/data/ --user=mysql

6、设置环境变量

shell> vi /root/.bash_profile
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin:/usr/local/mysql/lib
shell> source /root/.bash_profile

7、开启mysql服务

shell>  service mysql start
Starting MySQL....... [确定]
shell> chkconfig --add mysql //加入开机启动服务

8、修改MySQL的root用户的密码以及打开远程连接

shell> mysql -u root mysql
mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "passwd";
Query OK, 0 rows affected (0.09 sec)

mysql> update user set password=password('passwd') where user='root';

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit

shell>mysql -u root -p
Enter password:

不能通过嵌套文件方式连接

[1]

shell> mysql -u root mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
shell> ls /tmp/mysql.sock
ls: 无法访问/tmp/mysql.sock: 没有那个文件或目录
shell> ln -s /db/mysql/data/mysql.sock /tmp/mysql.sock
shell> ls /tmp/mysql.sock
/tmp/mysql.sock

[2]
修改配置文件/etc/my.cnf并重启mysql服务器
指定socket=/tmp/mysql.sock

[3]
通过TCP/IP连接
shell> mysql -h 127.0.0.1 -u root mysql

shell> mysql -u root mysql

整理自网络

Svoid
2014-07-18