MYSQL_Linux安装笔记
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MYSQL_Linux安装笔记相关的知识,希望对你有一定的参考价值。
mysql_Linux安装笔记
***实验环境***
1.系统平台:RedHat Linux 6.5
[[email protected] ~]# uname -a
Linux mysql 2.6.32-431.el6.x86_64 #1 SMP Sun Nov 10 22:19:54 EST 2013 x86_64 x86_64 x86_64 GNU/Linux
[[email protected] ~]# cat /etc/issue
Red Hat Enterprise Linux Server release 6.5 (Santiago)
Kernel \r on an \m
2.实用软件:mysql-5.5.37.tar.gz,cmake-2.8.12.2.tar.gz
软件来源:http://mysql.mirror.kangaroot.net/Downloads/MySQL-5.5/
http://wwwNaNake.org/files/v2.8/
3.准备前工作:
A.修改主机名文件
[[email protected] /]# vi /etc/hosts
192.168.1.211 mysql
B.修改网络配置文件
[[email protected] /]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=00:0C:29:A6:EF:6E
TYPE=Ethernet
UUID=4d0cd3ec-ef71-4036-bf9e-4d46ae76bcb2
ONBOOT=yes--不允许自动引导
NM_CONTROLLED=yes
BOOTPROTO=static--使用静态IP地址
IPADDR=192.168.1.221--添加Ip地址
NETWORK=255.255.255.0--添加子网掩码
GATEWAY=192.168.1.1--添加网关
[[email protected] /]# service network restart
B.关闭防火墙
[[email protected] /]# service iptables stop
C.关闭安全机制
[[email protected] /]# vi /etc/selinux/config
SELINUX=disabled
D.重启计算机
[[email protected] /]# reboot
or init 6
or shutdown -rf
一、 校验MYSQL是否存在已安装库文件,如果已存在库文件需删除;不然会引起冲突
[[email protected] ~]# rpm -qa | grep -i mysql
qt-mysql-4.6.2-26.el6_4.x86_64
mysql-server-5.1.71-1.el6.x86_64
mysql-libs-5.1.71-1.el6.x86_64
perl-DBD-MySQL-4.013-3.el6.x86_64
mysql-5.1.71-1.el6.x86_64
二、 卸载已存在的MYSQL库文件
[[email protected] ~]# rpm -ev qt-mysql-4.6.2-26.el6_4.x86_64 --nodeps
[[email protected] ~]# rpm -ev mysql-server-5.1.71-1.el6.x86_64 --nodeps
[[email protected] ~]# rpm -ev mysql-libs-5.1.71-1.el6.x86_64 --nodeps
[[email protected] ~]# rpm -ev mysql-5.1.71-1.el6.x86_64 --nodeps
[[email protected] ~]# rpm -ev perl-DBD-MySQL-4.013-3.el6.x86_64 --nodeps
OR 使用yum 删除库文件
/*6.5 yum配置方法*/
a. 备份系统自带yum源,备份之后;或删除或重命名
[[email protected] mysql]# cp –p /etc/yum.repos.d/rhel-source.repo ~/rhel-source.repo.bak
[[email protected] mysql]# rm –f /etc/yum.repos.d/rhel-source.repo
or
[[email protected] mysql]# mv rhel-source.repo back_rhel-source.repo.bak
b.创建yum源文件
[[email protected] mysql]# cd /etc/yum.repos.d
vi rhel6.repo
[rhel6.5]
name=yum server
baseurl=file:///yum
enabled=1
gpgcheck=0
gpgfile=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
c.创建yum目录
[[email protected] mysql]# mkdir /yum
d.挂在光驱至yum目录(redhat 6.5 配置完成后,如果不能识别需要重启服务器再挂载yum)
[[email protected] mysql]# mount /dev/cdrom /yum
f.测试yum源
[[email protected] mysql]# Yum list
[[email protected] mysql]# Yum clean all
g.删除MYSQL库文件
[[email protected] mysql]# yum –y remove mysql-5.1.71-1.el6.x86_64
三、 将动态函数库加载到高速缓存(cache)中,提供MYSQL的高可用性
1. 将MySQL数据库函数库的存放目录写入此文件中
[[email protected] software]# vi /etc/ld.so.conf
include ld.so.conf.d/*.conf
/usr/local/lib
/usr/local/lib64
2.保存写入,生效函数库
[[email protected] software]# ldconfig
[[email protected] software]# ldconfig -p
四、 确保以下所需系统软件包已经被安装
1. 切入到yum源存放目录
[[email protected] Packages]# cd /yum/Packages
2. 校验系统所需软件包gcc* gcc-c++* autoconf* automake* zlib* libxml* ncurses-devel* libgcrypt* libtool*
[[email protected] Packages]# rpm -qa | grep -i gcc*
3.好吧,不要都验证了;把所有的需求软件包都安装
[[email protected] Packages]# yum -y install gcc* gcc-c++* autoconf* automake* zlib* libxml* ncurses-devel* libgcrypt* libtool*
五、 安装方法常用有两种,一种为通用方法格式为.tar的源码安装,一种为RPM包安装方法
***源码包实施安装过程***
1. 查看MYSQL组和用户,重建MYSQL组及用户
[[email protected] ~]# cat /etc/group
[[email protected] ~]# cat /etc/passwd
[[email protected] ~]# userdel mysql
[[email protected] ~]# groupadd mysql
[[email protected] ~]# useradd -r -g mysql mysql
2. 新建MYSQL数据库工作目录,建议设在/usr/local/下面
a. 创建MYSQL安装路径
[[email protected] mysql]# mkdir -p /usr/local/mysql
b. 创建MYSQL数据库存放路径
[[email protected] /]# mkdir -p /data/mysql
c. 赋予MYSQL数据存放权限
[[email protected] /]# chown -R mysql:mysql /data/mysql/
[[email protected] /]# chmod 775 /data/mysql/
3. 安装cmake软件包
[[email protected] software]# tar -zxvf cmake-2.8.12.2.tar.gz
[[email protected] software]# ls
[[email protected] software]# cd cmake-2.8.12.2
[[email protected] cmake-2.8.12.2]# ls
[[email protected] cmake-2.8.12.2]# ./configure
[[email protected] cmake-2.8.12.2]# make;make install
4. 编译安装MYSQL源码包
[[email protected] software]# tar zxvf mysql-5.5.37.tar.gz
[[email protected] software]# cd mysql-5.5.37
[[email protected] mysql-5.5.37]# ls
[[email protected] mysql-5.5.37]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS:STRING=utf8,gbk -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/data/mysql/ -DMYSQL_USER=mysql -DMYSQL_TCP_PORT=3306
[[email protected] mysql-5.5.37]# echo $?
***注意事项***
echo $? 返回0则表示成功,返回1则标识失败
重新编译时,需要清除旧的对象文件和缓存信息。
# make clean
# rm -f CMakeCache.txt
# rm -rf /etc/my.cnf
参数说明:
-DCMAKE_INSTALL_PREFIX=/home/mysql/mysql //安装目录
-DINSTALL_DATADIR=/home/mysql/data //数据库存放目录
-DDEFAULT_CHARSET=utf8 //使用utf8字符
-DDEFAULT_COLLATION=utf8_general_ci //校验字符
-DEXTRA_CHARSETS=all //安装所有扩展字符集
-DENABLED_LOCAL_INFILE=1 //允许从本地导入数据
[[email protected] mysql-5.5.37]# make;make install
[[email protected] mysql-5.5.37]# echo $? (0表示成功,1表示失败)
5. /usr/local/src/mysql/mysql-5.5.37下的目录结构
Directory | Contents of Directory |
bin | Client programs and the mysqld server |
data | Log files, databases |
docs | Manual in Info format |
man | Unix manual pages |
include | Include (header) files |
lib | Libraries |
scripts | mysql_install_db |
share | Miscellaneous support files, including error messages, sample configuration files, SQL for database installation |
sql-bench | Benchmarks |
6. 配置并初始化数据库
a. 创建my.cnf配置文件
[[email protected] mysql-5.5.37]# cp support-files/my-medium.cnf /etc/my.cnf
b. 执行前需赋给scripts/mysql_install_db文件执行权限
[[email protected] mysql-5.5.37]# cd /usr/local/mysql/
[[email protected] mysql]# ls
c. 初始化数据库
[[email protected] mysql]# scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/
[[email protected] mysql]# echo $?
d. 创建管理MySQL数据库的shell脚本,并赋予其执行权限
[[email protected] mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[[email protected] mysql]# chmod +x /etc/init.d/mysqld
e. 启动MYSQL数据库
[[email protected] mysql]# /etc/init.d/mysqld start
Starting MySQL...[ OK ]
f. 通过命令行登录管理MySQL服务器(提示输入密码时直接回车)
[[email protected] mysql]# /usr/local/mysql/bin/mysql -u root –p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.37-log Source distribution
Copyright (c) 2000, 2014, 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.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
mysql> quit
Bye
g. 安装MYSQL客户端软件包,可以支持MYSQL命令行命令
[[email protected] Packages]# yum -y install mysql
h. 设置Mysql密码
[[email protected] ~]#/usr/local/mysql/bin/mysqladmin password -uroot ‘cy_mysql‘
***遇到问题,提示没有使用密码***
mysqladmin: connect to server at ‘localhost‘ failed
error: ‘Access denied for user ‘root‘@‘localhost‘ (using password: YES)‘
***解决办法***
(1)查看配置文件使用密码情况
[[email protected] Packages]# cat /etc/my.cnf
(2)建立mysql命令链接
[[email protected] mysql]# ln -s /usr/local/mysql/bin/* /usr/local/bin
(3)以命令行模式启动MYSQL,修改密码,为root添加远程连接能力;更新权限
[[email protected] mysql]# mysqld_safe --skip-grant-tables &
mysql -uroot –p
Enter password:cy_mysql (输入密码)
mysql> use mysql;
mysql> GRANT ALL PRIVILEGES ON *.* TO [email protected]"%" IDENTIFIED BY "root";
or
mysql>GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘%‘ IDENTIFIED BY ‘cy_mysql‘ WITH GRANT OPTION;
mysql> update user set password=PASSWORD("cy_mysql")where user="root";
mysql> select Host,User,Password from user where User=‘root‘;
mysql> quit
(4)重新启动MYSQL数据库,并验证登录
[[email protected] ~]# service mysqld restart
[[email protected] ~]# mysql -uroot –p
Enter password:cy_mysql
mysql> show databases ;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> quit
7. 重新启动Linux服务器,登录MYSQL报错
[[email protected] ~]# mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can‘t connect to local MySQL server through socket ‘/data/mysql/mysql.sock‘ (2)
a. 查看MYSQL数据服务是否启动
[[email protected] ~]# /etc/init.d/mysqld status
b. 启动MYSQL数据库
[[email protected] ~]# service mysqld start
Or
[[email protected] ~]# /etc/init.d/mysqld start
8. 在客户端使用Navicat for mysql工具连接MYSQL进行操作
9. 修改MYSQL服务器数据库中命令行不区分表大小写
[[email protected] ~]# vi /etc/my.cnf
[mysqld]
lower_case_table_names=1
10. 数据备份与还原
(1). 备份整个库
a. 创建备份目录
[[email protected] /]# mkdir /backup
b. 指定备份目录,指定备份库
[[email protected] /]# mysqldump -u root -p bank > /backup/test_bank.sql
Enter password:
c. 还原数据库
[[email protected] backup]# mysql -u root -p
Enter password:
mysql> use bank
Database changed
mysql> source /backup/test_bank.sql
(2).备份某一个表
a.备份表
[[email protected] backup]# mysqldump -u root -p bank tab_item > /backup/test_bank_item.sql
c. 还原表
[[email protected] backup]# mysql -u root -p
Enter password:
mysql> use bank
Database changed
mysql> source /backup/test_bank_item.sql
以上是关于MYSQL_Linux安装笔记的主要内容,如果未能解决你的问题,请参考以下文章