Linux CentOS 6.x 开发配置文档 6 - MySQL

一,默认安装:
1,安装MySQL客户端:yum install mysql
2,安装MySQL服务端:yum install mysql-server mysql-devel
3,设置字符集:vim /etc/my.cnf,加入:default-character-set=utf-8
4,
设置开机启动:chkconfig mysqld on
5,查看:chkconfig --list |  grep mysql*
6,开启服务:/etc/rc.d/init.d/mysqld start(service mysqld start、service msyqld status
service mysqld stop
7
默认root没有密码,直接进去:mysql -u root,设置密码:set password for root@localhost=password('123456');
8,添加远程登录:
grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;flush privileges;
(如果启动服务后出现:MySQL Daemon failed to start.查看日志的问题是:
Can't open and lock privilege tables: Table 'mysql.host' doesn't exist,解决方法:/usr/bin/mysql_install_db --user=mysql --datadir=/var/lib/mysql,意思是:将数据目录不一致)

二,源代码编译安装:
1,先检查:
rpm -qa | grep mysql
2,如果有就删除:
yum -y remove mysql...* 
3,安装依赖:
yum -y install make gcc-c++ cmake bison-devel ncurses-devel
4,下载: wget  http://cdn.mysql.com/Downloads/MyS QL-5.6/ mysql-5.6.16.tar.gz
5,解压: tar xvf mysql-5.6.16.tar.gz
6,进去: cd mysql-5.6.16
7,CMAKE:cmake -DCMAKE_INSTALL_PREFIX=/home/MySQL -DMYSQL_DATADIR=/home/MySQL/data -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
8,编译:make
9,编译安装:make install
10,检查是否有mysql用户:
cat /etc/passwd
11, 检查是否有mysql用户组:cat /etc/group
12,如果没有就创建:
groupadd mysql useradd -g mysql mysq l
13,修改权限:
chown -R mysql:mysql /home/MySQL
14, 进入/home/MySQL,执行初始化配置脚本并创建系统自带的数据库和表: scripts/mysql_install_db --basedir=/home/MySQL --datadir=/home/MySQL/data --user=mysql
15,拷贝mysql服务到 init.d目录: cp support-files/mysql.server /etc/init.d/mysql
16,设置开机启动: chkconfig mysqld on
17,
开启服务:/etc/rc.d/init.d/mysqld start(service mysqld start、service msyqld statusservice mysqld stop
18,
设置root密码:mysql -u root,设置密码:set password for root@localhost=password('123456');
19,
添加远程登录:grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;flush privileges;
20,设置环境变量:
    1:编辑环境变量:vim /etc/profile
    2:直接在JDK的PATH后添加,即:export PATH=$PATH:$JAVA_HOME/bin:/home/MySQL/bin:$PATH
    3:让环境变量生效:source /etc/profile
 

 

MySQL数据库备份:mysqldump -h127.0.0.1 -u root -p mysql > /root/Desktop/db.sql;(Windows下的备份如下:mysqldump.exe -h127.0.01 -u root -p mysql > d:\
mysql.sql)
将查询输出到文件:mysql -u root -p (加不加数据库名都可以) -e "show global status" > /root/Desktop/sql.log;
mysql -u root -p 数据库 -e "select * from 表" > /root/Desktop/sql.log; 
mysql -u root -p 数据库 -e "select * from 表 into outfile '/tmp/sql' fields terminated by '\t' lines terminated by '\r\n'";
 

MySQL数据库还原:source /root/Desktop/db.sql;

常见错误:
一,忘记密码:

    1,关闭服务:service mysqld stop
    2,进入安全模式:mysqld_safe --user=root --skip-grant-tables
    3,进入数据库:msyql -u root -p
    4,进入mysql库:use mysql
    5,重置密码:update user set password=password("123456") where user="root";
    6,flush: flush privileges;

 

二,
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'
    
1,检查服务:service mysqld status或/etc/rc.d/init.d/mysqld status
    2,
建立软连接:ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
 

三,让MySQL不区分大小写
1,编辑:vim /etc/my.cnf,添加:lower_case_table_names=1

四,查询MySQL的大小写:
show variables like "%case%";
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| lower_case_file_system | OFF   | 
| lower_case_table_names | 0     |     //0区分 1不区分
+------------------------+-------+

2 rows in set (0.00 sec)



附:Windows下使用压缩版的MySQL
1、到官网下载最新的MySQL版本,并解压到任意路径,如:D:\MySQL
2、设置环境变量:MYSQL_HOME,值为你解压的路径:D:\MySQL
3、在系统环境变量的path中添加:%MYSQL_HOME%\bin

4、打开
MySQL根目录,会发现有my-default.ini文件,复制一份,然后重命名为:my.ini
5、在my.ini中添加如下内容:
[mysqld]
#设置字符集为utf8
loose-default-character-set = utf8
basedir = D:/Program Files/mysql/mysql-5.6.11-win32
datadir = D:/Program Files/mysql/mysql-5.6.11-win32/data
 
[client]
loose-default-character-set = utf8
 
[mysql]
loose-default-character-set = utf8
 
[WinMySQLadmin]
server = D:\MySQL\bin\mysqld.exe

6,在DOS中进入:
D:\MySQL\bin目录,执行: mysqld -install,安装mysql服务,执行成功后会有:service successfully installed。卸载的话,使用:mysqld -remove

7,启动MySQL服务:net start mysql

8、默认情况下root是空密码,给root设置密码:mysqladmin -u root -p password "新密码"

9、然后在命令提示符下执行MySQL命令既可进入MySQL,然后进行各种操作(建表、查询之类的)

你可能感兴趣的:(Linux CentOS 6.x 开发配置文档 6 - MySQL)