@mrz1
2018-01-23T09:17:58.000000Z
字数 6087
阅读 870
笔记
安装mariadb-server
yum install mariadb-server
查看安装文件
rpm -ql MariaDb-server
rpm -ql MariaDb-Galera-server
启动服务 端口3306
systemctl start mariadb (centos7)
chkconfig --list mysql
service mysql start
客户端
[root@centos7 ~]#which mysql
/usr/bin/mysql
[root@centos7 ~]#rpm -qf /usr/bin/mysql
MariaDB-client-10.2.12-1.el7.centos.x86_64
[root@centos7 ~]#rpm -ql MariaDB-client
所有MySQL命令列表:
注意,所有文本命令必须在第一行,以";"结尾;
? (\?) 帮助
clear (\c) 清除当前输入语句。
connect (\r) Reconnect to the server. Optional arguments are db and host.(重新连接服务器。可选参数是db和主机。)
delimiter (\d) Set statement delimiter.(语句分隔符)
edit (\e) Edit command with $EDITOR.(编辑命令与$编辑器。)
ego (\G) Send command to mysql server, display result vertically.(向MySQL服务器发送命令,垂直显示结果。)
exit (\q) Exit mysql. Same as quit.(退出MySQL。)
go (\g) Send command to mysql server.(发送命令到MySQL服务器。)
help (\h) Display this help.(显示此帮助。)
nopager (\n) Disable pager, print to stdout.(打印到标准输出。)
notee (\t) Don't write into outfile.(不要写进文件。)
pager (\P) Set PAGER [to_pager]. Print the query results via PAGER.
print (\p) Print current command.(打印当前命令。)
prompt (\R) Change your mysql prompt.(更改MySQL提示。)
quit (\q) Quit mysql.(退出)
rehash (\#) Rebuild completion hash.
source (\.) Execute an SQL script file. Takes a file name as an argument.(执行SQL脚本文件。以文件名作为参数。)
status (\s) Get status information from the server.(从服务器获取状态信息。)
system (\!) Execute a system shell command.(执行系统shell命令)
tee (\T) Set outfile [to_outfile]. Append everything into given outfile.
use (\u) Use another database. Takes database name as argument. (使用另一个数据库。以数据库名称作为参数。)
charset (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.(切换到另一个字符集。可能是多字节字符集的处理需要与binlog。)
warnings (\W) Show warnings after every statement.(每次声明后显示警告。)
nowarning (\w) Don't show warnings after every statement.(每次声明后不要显示警告。)
实例1 修改mysql默认提示符,修改文件/etc/my.cnf.d/mysql-clients.cnf
[root@centos7 ~]#vim /etc/my.cnf.d/mysql-clients.cnf
[mysql]
prompt="(\u@\h)\D [\d]>" //修改提示符 这里修改的是时间
运行结果:
[root@centos7 ~]#mysql
(root@localhost)Tue Jan 23 14:45:37 2018 [(none)]>
实例2
use mysql 连接数据库
show databases; 查看数据库列表
drop database test; 删除数据库
select user(); 查看当前用户身份
centos6 (5.5版本)
安装mariadb-server
yum install mariadb-server
查看安装文件
rpm -ql MariaDb-Galera-server
启动服务 端口3306
chkconfig --list mysql
service mysql start (程序名mysqld多线程)
客户端
[root@centos7 ~]#which mysql
/usr/bin/mysql
[root@centos7 ~]#rpm -qf /usr/bin/mysql
MariaDB-client-10.2.12-1.el7.centos.x86_64
[root@centos7 ~]#rpm -ql MariaDB-client
第一步:准备账号
[root@centos7 ~]#getent passwd mysql //查看有没有mysql账号(没有的话需要创建)
[root@centos7 ~]#useradd -r mysql -s /sbin/nologin //创建系统账号 shell类型为nologin
[root@centos7 ~]#getent passwd mysql
mysql:x:988:983::/home/mysql:/sbin/nologin
[root@centos7 ~]#id mysql
uid=988(mysql) gid=983(mysql) groups=983(mysql)
第二步:解压缩包
1.解压缩包
[root@centos7 ~]#tar xf mariadb-10.2.12-linux-x86_64.tar.gz -C /usr/local/ //解压路径必须的/usr/local/
[root@centos7 ~]#cd /usr/local/;ls
bin games lib libexec sbin src etc include lib64 mariadb-10.2.12-linux-x86_64 share
2.创建软连接
[root@centos7 local]#ln -s mariadb-10.2.12-linux-x86_64/ mysql //建议给mariadb-10.2.12-linux-x86_64/ 加上软连接必须是mysql或者此文件改名为mysql
3.修改权限问题
[root@centos7 local]#cd mysql/
[root@centos7 mysql]#ll
drwxrwxr-x 2 1021 zhang1 4096 Nov 14 22:34 bin
-rw-r--r-- 1 1021 zhang1 17987 Jan 3 21:48 COPYING
·················
解决如下
[root@centos7 local]#chown -R mysql.mysql mysql/
[root@centos7 local]#ll mysql/
drwxrwxr-x 2 mysql mysql 4096 Nov 14 22:34 bin
-rw-r--r-- 1 mysql mysql 17987 Jan 3 21:48 COPYING
·················
4.添加PATH
[root@centos7 bin]#echo 'PATH=/usr/local/mysql/bin:$PATH' >/etc/profile.d/mysql.sh
[root@centos7 bin]#cat /etc/profile.d/mysql.sh
PATH=/usr/local/mysql/bin:$PATH
[root@centos7 bin]#. /etc/profile.d/mysql.sh
第三步:数据库目录存放路径
考虑数据一直在增加建议放到逻辑卷中;下面开始创建逻辑卷
创建逻辑卷不懂得可以看前面文章查看怎么创建逻辑卷
[root@centos7 /]#blkid
/dev/sda6: UUID="eeDr4z-UEpo-t6K6-BzAB-IwfG-1rhK-iTHhbx" TYPE="LVM2_member"
/dev/mapper/vg0-lv_mysqldata: UUID="f1fab2a8-24f6-4752-b0ae-62b35f205f27" TYPE="xfs"
[root@centos7 /]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
······
└─sda6 8:6 0 50G 0 part
└─vg0-lv_mysqldata 253:0 0 50G 0 lvm
以上是创建的逻辑卷
mkdir /deta/mysqldb -pv 创建data存放的目录
修改权限
[root@centos7 bin]#ll -d /data/mysqldb/
drwxr-xr-x 2 root root 6 Jan 23 15:47 /data/mysqldb/
[root@centos7 bin]#chown -R mysql.mysql /data/mysqldb/
[root@centos7 bin]#ll -d /data/mysqldb/
drwxr-xr-x 2 mysql mysql 6 Jan 23 15:47 /data/mysqldb/
[root@centos7 bin]#chmod 700 /data/mysqldb/
[root@centos7 bin]#ll -d /data/mysqldb/
drwx------ 2 mysql mysql 6 Jan 23 15:47 /data/mysqldb/
第四步:执行脚本
[root@centos7 ~]#cd /usr/local/mysql/scripts/
[root@centos7 scripts]#./mysql_install_db --help 查看帮助
[root@centos7 mysql]#scripts/mysql_install_db --datadir=/data/mysqldb --user=mysql 执行此脚本 必须是在mysql目录下
[root@centos7 support-files]#cp my-huge.cnf /etc/my.cnf
[root@centos7 support-files]#vim /etc/my.cnf
[mysqld]
port = 3306
datadir=/data/mysqldb 这里添加数据路径 socket = /tmp/mysql.sock ·······
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld //配置文件
[root@centos7 support-files]#chkconfig --add mysqld //添加到启动脚本
[root@centos7 support-files]#chkconfig --list //查看是否添加
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
第五步: 启动
[root@centos7 support-files]#systemctl start mysqld //启动
You have new mail in /var/spool/mail/root
[root@centos7 support-files]#systemctl status mysqld
·····
Active: active (running) since Tue 2018-01-23 16:38:23 CST; 4s
·····
[root@centos7 support-files]#ss -ntl //查看端口3306 启动成功
LISTEN 0 80 :::3306 :::*
服务启动会生成/tmp/mysql.sock 文件;服务停止删除此文件
第六步:安全初始化
数据库默认安装后
MariaDB [mysql]> select user,host from mysql.user;
+------+-------------------+
| user | host |
+------+-------------------+
| root | 127.0.0.1 |
| root | ::1 |
| | centos7.qifei.com |
| root | centos7.qifei.com |
| | localhost |
| root | localhost |
+------+-------------------+
6 rows in set (0.00 sec)
MariaDB [mysql]> select user,host password from mysql.user;
+------+-------------------+
| user | password |
+------+-------------------+
| root | 127.0.0.1 |
| root | ::1 |
| | centos7.qifei.com |
| root | centos7.qifei.com |
| | localhost |
| root | localhost |
+------+-------------------+
6 rows in set (0.00 sec)
跑脚本
[root@centos7 ~]#mysql_secure_installation (安全初始化)
请输入root口令,你没有直接回车
Enter current password for root (enter for none):
是否设置root口令
Set root password? [Y/n] y
New password:
Re-enter new password:
是否删除匿名账户
Remove anonymous users? [Y/n] y
是否禁用root用户远程连接
Disallow root login remotely? [Y/n] n
是否删除test测试文件
Remove test database and access to it? [Y/n] y
是否生效保存
Reload privilege tables now? [Y/n] y
注意事项
[fei@centos7 ~]$mysql -uroot -p 普通用户用root登录
Enter password:
MariaDB [(none)]> system cat /etc/passwd //能打开所有文件 并且能修改root密码
root:x:0:0:root:/root:/bin/bash
···········
所以 mysql的root口令千万别泄露