一:mysql的编译和安装
1:准备工作
[root@localhost ~]# rpm -q mysql-server mysql //查看mysql是否已经安装
[root@localhost ~]# cd /media/cdrom/Packages/ //进入光盘所在目录
[root@localhost Packages]# rpm -ivh ncurses-devel-5.7-3.20090208.el6.x86_64.rpm //安装软件
ncurses是字符终端下屏幕控制的基本库,包括面板和菜单功能,在TTY下登录到主机上mysql需要的。
[root@localhost Packages]# cd
[root@localhost ~]# tar zxvf cmake-2.8.6.tar.gz //解包
[root@localhost ~]# cd cmake-2.8.6
[root@localhost cmake-2.8.6]# ./configure //配置
[root@localhost cmake-2.8.6]# gmake && gmake install //编译安装
2:安装mysql
[root@localhost cmake-2.8.6]# cd
[root@localhost ~]# groupadd mysql
[root@localhost ~]# useradd -M -s /sbin/nologin mysql -g mysql
[root@localhost ~]# cd /usr/src
[root@localhost src]# tar zxvf mysql-5.6.36.tar.gz
[root@localhost src]# cd mysql-5.6.36
[root@localhost mysql-5.6.36]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all
-DDEFAULT_COLLATION:用于指定数据集如何排序,以及字符串的比对规则
[root@localhost mysql-5.6.36]# make
[root@localhost mysql-5.6.36]# make install
3:安装后的调整
[root@localhost mysql-5.6.36]# cd
[root@localhost ~]# chown -R mysql:mysql /usr/local/mysql/
[root@localhost ~]# rm -rf /etc/my.cnf
[root@localhost ~]# cd mysql-5.6.36
[root@localhost mysql-5.6.36]# cp support-files/my-default.cnf /etc/my.cnf
4:初始化数据库
[root@localhost mysql-5.6.36]# yum -y install autoconf
[root@www mysql-5.6.36]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/
[root@localhost mysql-5.6.36]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
[root@localhost mysql-5.6.36]# . /etc/profile \\点后有个空格
5:添加系统服务
方法1:
[root@localhost mysql-5.6.36]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@localhost mysql-5.6.36]# chmod +x /etc/rc.d/init.d/mysqld
[root@localhost mysql-5.6.36]# chkconfig --add mysqld
[root@localhost mysql-5.6.36]# service mysqld start
[root@localhost mysql-5.6.36]# /etc/init.d/mysqld status
[root@localhost mysql-5.6.36]# netstat -anpt | grep mysqld
[root@localhost mysql-5.6.36]# mysqladmin -u root password 'pwd123'
或
mysql> set password=password('pwd123');
方法2:
[root@localhost ~]# cd mysql-5.6.36/
[root@localhost mysql-5.6.36]# cp support-files/mysql.server /usr/local/mysql/bin/mysqld.sh
[root@localhost mysql-5.6.36]# chmod +x /usr/local/mysql/bin/mysqld.sh
root@localhost ~]# vi /usr/lib/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
After=network.target
[Service]
User=mysql
Group=mysql
Type=forking
PIDFile=/usr/local/mysql/data/mysql.pid
ExecStart=/usr/local/mysql/bin/mysqld.sh start
ExecStop=/usr/local/mysql/bin/mysqld.sh stop
[Install]
WantedBy=multi-user.target
6:访问mysql
[root@localhost ~]# mysql -u root -p
mysql> show master logs;
mysql> exit
二:使用mysql数据库
1:查看数据库结构
1):查看当前服务器中有哪些数据库
[root@localhost mysql-5.6.36]# mysql -u root //登录数据库
mysql> show databases; //查看数据库
2):查看当前数据库中的表
mysql> use mysql; //使用mysql数据库
mysql> show tables; //查看数据表
3):查看表的结构
mysql> describe user; //查看表结构
2:创建及删除库和表
1):创建新的库
mysql> create database auth;
2):创建新的表
mysql> use auth;
mysql> create table users (user_name char(16) not null,user_passwd char(48) default '',primary key (user_name));
如果在创建表时没有设置主键,可以用下面语句设置
添加主键
mysql>ALTER TABLE users ADD CONSTRAINT PK_users PRIMARY KEY (user_name);
删除主键
mysql>Alter table users drop primary key;
3):删除数据表
mysql> drop table auth.users;
4):删除数据库
mysql> drop database auth;
3:管理表中的数据
1):插入数据记录
mysql> use auth;
mysql> insert into users(user_name,user_passwd) values('zhangsan',password('123456'));
mysql> insert into users values('lisi',password('123456'));
2)查询数据记录
mysql> select * from auth.users;
mysql> select * from users;
mysql> select user_name,user_passwd from auth.users where user_name='zhangsan';
3)修改数据记录
mysql> update auth.users set user_passwd=password('') where user_name='lisi';
mysql> select * from users;
mysql> update mysql.user set password=password('benet') where user='root';
或
[root@localhost mysql-5.6.36]# mysqladmin -u root -p password 'aptech'
设置mysql.user表中的用户密码时必须用加密的方法
4):删除数据记录
mysql> delete from auth.users where user_name='lisi';
mysql> select * from users;
mysql> select user,host,password from mysql.user where user='';
mysql> delete from mysql.user where user='';
三:mysql的维护
1:数据库用户的授权
1):添加账户
mysql>create user zhangsan;
mysql>update mysql.user set password=password('benet') where user='zhangsan';
mysql>flush privileges;
2):授予权限
mysql> grant select on auth.* to 'xiaoqi'@'localhost' identified by 'aptech'; \\添加用户并授予权限
mysql>exit
[root@localhost ~]#mysql –u xiaoqi -p
mysql> select * from auth.users;
mysql> select * from mysql.user; \\无权查看
mysql> exit
[root@localhost mysql-5.6.36]# mysql -u root -p
mysql> create database bdqn;
mysql> grant all on bdqn.* to 'dbuser'@'192.168.4.19' identified by 'aptech';
3):查看权限
mysql> show grants for 'dbuser'@'192.168.4.19';
4):权限的撤销
mysql> revoke all on auth.* from 'xiaoqi'@'localhost';
mysql> show grants for 'xiaoqi'@'localhost';
2:数据库的备份与恢复(导入导出)
1):备份数据库
[root@localhost ~]# mysqldump -u root -p auth users > /opt/authl-users.sql \\导出数据表
[root@localhost ~]# ls
[root@localhost ~]# mysqldump -u root -p --database auth > /opt/auth.sql \\导出数据库
[root@localhost ~]# ls
[root@localhost ~]# grep -v "^--" auth.sql | grep -v "^/" | grep -v "^$" \\查看备份
2):恢复
(1)将表导入到另一个表
[root@localhost ~]# mysql -u root -p test < /opt/auth-users.sql \\导入到另一个数据库
[root@localhost ~]# mysql -u root -p
mysql> use test;
mysql> show tables;
(2)将表导入到原来所在的数据库
mysql>use auth;
mysql> drop table auth.users; \\可以先将表删掉,以验证结果
[root@localhost ~]# mysql -u root -p auth</opt/auth-users.sql
(3)导入数据库
[root@localhost data]# mysql -u root -p < /opt/ auth.sql
[root@localhost data]# ls -ld auth