[关闭]
@mrz1 2018-01-23T09:17:58.000000Z 字数 6087 阅读 756

2017-1-19(MySQL 安装)

笔记


centos7(10.0版本)

  1. 安装mariadb-server
  2. yum install mariadb-server
  3. 查看安装文件
  4. rpm -ql MariaDb-server
  5. rpm -ql MariaDb-Galera-server
  6. 启动服务 端口3306
  7. systemctl start mariadb (centos7)
  8. chkconfig --list mysql
  9. service mysql start
  10. 客户端
  11. [root@centos7 ~]#which mysql
  12. /usr/bin/mysql
  13. [root@centos7 ~]#rpm -qf /usr/bin/mysql
  14. MariaDB-client-10.2.12-1.el7.centos.x86_64
  15. [root@centos7 ~]#rpm -ql MariaDB-client
  16. 所有MySQL命令列表:
  17. 注意,所有文本命令必须在第一行,以";"结尾;
  18. ? (\?) 帮助
  19. clear (\c) 清除当前输入语句。
  20. connect (\r) Reconnect to the server. Optional arguments are db and host.(重新连接服务器。可选参数是db和主机。)
  21. delimiter (\d) Set statement delimiter.(语句分隔符)
  22. edit (\e) Edit command with $EDITOR.(编辑命令与$编辑器。)
  23. ego (\G) Send command to mysql server, display result vertically.(向MySQL服务器发送命令,垂直显示结果。)
  24. exit (\q) Exit mysql. Same as quit.(退出MySQL。)
  25. go (\g) Send command to mysql server.(发送命令到MySQL服务器。)
  26. help (\h) Display this help.(显示此帮助。)
  27. nopager (\n) Disable pager, print to stdout.(打印到标准输出。)
  28. notee (\t) Don't write into outfile.(不要写进文件。)
  29. pager (\P) Set PAGER [to_pager]. Print the query results via PAGER.
  30. print (\p) Print current command.(打印当前命令。)
  31. prompt (\R) Change your mysql prompt.(更改MySQL提示。)
  32. quit (\q) Quit mysql.(退出)
  33. rehash (\#) Rebuild completion hash.
  34. source (\.) Execute an SQL script file. Takes a file name as an argument.(执行SQL脚本文件。以文件名作为参数。)
  35. status (\s) Get status information from the server.(从服务器获取状态信息。)
  36. system (\!) Execute a system shell command.(执行系统shell命令)
  37. tee (\T) Set outfile [to_outfile]. Append everything into given outfile.
  38. use (\u) Use another database. Takes database name as argument. (使用另一个数据库。以数据库名称作为参数。)
  39. charset (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.(切换到另一个字符集。可能是多字节字符集的处理需要与binlog。)
  40. warnings (\W) Show warnings after every statement.(每次声明后显示警告。)
  41. nowarning (\w) Don't show warnings after every statement.(每次声明后不要显示警告。)

实例

实例1 修改mysql默认提示符,修改文件/etc/my.cnf.d/mysql-clients.cnf

  1. [root@centos7 ~]#vim /etc/my.cnf.d/mysql-clients.cnf
  2. [mysql]
  3. prompt="(\u@\h)\D [\d]>" //修改提示符 这里修改的是时间

运行结果:

  1. [root@centos7 ~]#mysql
  2. (root@localhost)Tue Jan 23 14:45:37 2018 [(none)]>

实例2

  1. use mysql 连接数据库
  2. show databases; 查看数据库列表
  3. drop database test 删除数据库
  4. select user(); 查看当前用户身份

centos6 (5.5版本)

  1. 安装mariadb-server
  2. yum install mariadb-server
  3. 查看安装文件
  4. rpm -ql MariaDb-Galera-server
  5. 启动服务 端口3306
  6. chkconfig --list mysql
  7. service mysql start (程序名mysqld多线程)
  8. 客户端
  9. [root@centos7 ~]#which mysql
  10. /usr/bin/mysql
  11. [root@centos7 ~]#rpm -qf /usr/bin/mysql
  12. MariaDB-client-10.2.12-1.el7.centos.x86_64
  13. [root@centos7 ~]#rpm -ql MariaDB-client

二进制安装mariadb

第一步:准备账号

  1. [root@centos7 ~]#getent passwd mysql //查看有没有mysql账号(没有的话需要创建)
  2. [root@centos7 ~]#useradd -r mysql -s /sbin/nologin //创建系统账号 shell类型为nologin
  3. [root@centos7 ~]#getent passwd mysql
  4. mysql:x:988:983::/home/mysql:/sbin/nologin
  5. [root@centos7 ~]#id mysql
  6. uid=988(mysql) gid=983(mysql) groups=983(mysql)

第二步:解压缩包

  1. 1.解压缩包
  2. [root@centos7 ~]#tar xf mariadb-10.2.12-linux-x86_64.tar.gz -C /usr/local/ //解压路径必须的/usr/local/
  3. [root@centos7 ~]#cd /usr/local/;ls
  4. bin games lib libexec sbin src etc include lib64 mariadb-10.2.12-linux-x86_64 share
  5. 2.创建软连接
  6. [root@centos7 local]#ln -s mariadb-10.2.12-linux-x86_64/ mysql //建议给mariadb-10.2.12-linux-x86_64/ 加上软连接必须是mysql或者此文件改名为mysql
  7. 3.修改权限问题
  8. [root@centos7 local]#cd mysql/
  9. [root@centos7 mysql]#ll
  10. drwxrwxr-x 2 1021 zhang1 4096 Nov 14 22:34 bin
  11. -rw-r--r-- 1 1021 zhang1 17987 Jan 3 21:48 COPYING
  12. ·················
  13. 解决如下
  14. [root@centos7 local]#chown -R mysql.mysql mysql/
  15. [root@centos7 local]#ll mysql/
  16. drwxrwxr-x 2 mysql mysql 4096 Nov 14 22:34 bin
  17. -rw-r--r-- 1 mysql mysql 17987 Jan 3 21:48 COPYING
  18. ·················
  19. 4.添加PATH
  20. [root@centos7 bin]#echo 'PATH=/usr/local/mysql/bin:$PATH' >/etc/profile.d/mysql.sh
  21. [root@centos7 bin]#cat /etc/profile.d/mysql.sh
  22. PATH=/usr/local/mysql/bin:$PATH
  23. [root@centos7 bin]#. /etc/profile.d/mysql.sh

第三步:数据库目录存放路径

  1. 考虑数据一直在增加建议放到逻辑卷中;下面开始创建逻辑卷
  2. 创建逻辑卷不懂得可以看前面文章查看怎么创建逻辑卷
  3. [root@centos7 /]#blkid
  4. /dev/sda6: UUID="eeDr4z-UEpo-t6K6-BzAB-IwfG-1rhK-iTHhbx" TYPE="LVM2_member"
  5. /dev/mapper/vg0-lv_mysqldata: UUID="f1fab2a8-24f6-4752-b0ae-62b35f205f27" TYPE="xfs"
  6. [root@centos7 /]#lsblk
  7. NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
  8. sda 8:0 0 200G 0 disk
  9. ······
  10. └─sda6 8:6 0 50G 0 part
  11. └─vg0-lv_mysqldata 253:0 0 50G 0 lvm
  12. 以上是创建的逻辑卷
  13. mkdir /deta/mysqldb -pv 创建data存放的目录
  14. 修改权限
  15. [root@centos7 bin]#ll -d /data/mysqldb/
  16. drwxr-xr-x 2 root root 6 Jan 23 15:47 /data/mysqldb/
  17. [root@centos7 bin]#chown -R mysql.mysql /data/mysqldb/
  18. [root@centos7 bin]#ll -d /data/mysqldb/
  19. drwxr-xr-x 2 mysql mysql 6 Jan 23 15:47 /data/mysqldb/
  20. [root@centos7 bin]#chmod 700 /data/mysqldb/
  21. [root@centos7 bin]#ll -d /data/mysqldb/
  22. drwx------ 2 mysql mysql 6 Jan 23 15:47 /data/mysqldb/

第四步:执行脚本

  1. [root@centos7 ~]#cd /usr/local/mysql/scripts/
  2. [root@centos7 scripts]#./mysql_install_db --help 查看帮助
  3. [root@centos7 mysql]#scripts/mysql_install_db --datadir=/data/mysqldb --user=mysql 执行此脚本 必须是在mysql目录下
  4. [root@centos7 support-files]#cp my-huge.cnf /etc/my.cnf
  5. [root@centos7 support-files]#vim /etc/my.cnf
  6. [mysqld]
  7. port = 3306
  8. datadir=/data/mysqldb 这里添加数据路径 socket = /tmp/mysql.sock ·······
  9. cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld //配置文件
  10. [root@centos7 support-files]#chkconfig --add mysqld //添加到启动脚本
  11. [root@centos7 support-files]#chkconfig --list //查看是否添加
  12. mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off

第五步: 启动

  1. [root@centos7 support-files]#systemctl start mysqld //启动
  2. You have new mail in /var/spool/mail/root
  3. [root@centos7 support-files]#systemctl status mysqld
  4. ·····
  5. Active: active (running) since Tue 2018-01-23 16:38:23 CST; 4s
  6. ·····
  7. [root@centos7 support-files]#ss -ntl //查看端口3306 启动成功
  8. LISTEN 0 80 :::3306 :::*
  9. 服务启动会生成/tmp/mysql.sock 文件;服务停止删除此文件

第六步:安全初始化

  1. 数据库默认安装后
  2. MariaDB [mysql]> select user,host from mysql.user;
  3. +------+-------------------+
  4. | user | host |
  5. +------+-------------------+
  6. | root | 127.0.0.1 |
  7. | root | ::1 |
  8. | | centos7.qifei.com |
  9. | root | centos7.qifei.com |
  10. | | localhost |
  11. | root | localhost |
  12. +------+-------------------+
  13. 6 rows in set (0.00 sec)
  14. MariaDB [mysql]> select user,host password from mysql.user;
  15. +------+-------------------+
  16. | user | password |
  17. +------+-------------------+
  18. | root | 127.0.0.1 |
  19. | root | ::1 |
  20. | | centos7.qifei.com |
  21. | root | centos7.qifei.com |
  22. | | localhost |
  23. | root | localhost |
  24. +------+-------------------+
  25. 6 rows in set (0.00 sec)
  26. 跑脚本
  27. [root@centos7 ~]#mysql_secure_installation (安全初始化)
  28. 请输入root口令,你没有直接回车
  29. Enter current password for root (enter for none):
  30. 是否设置root口令
  31. Set root password? [Y/n] y
  32. New password:
  33. Re-enter new password:
  34. 是否删除匿名账户
  35. Remove anonymous users? [Y/n] y
  36. 是否禁用root用户远程连接
  37. Disallow root login remotely? [Y/n] n
  38. 是否删除test测试文件
  39. Remove test database and access to it? [Y/n] y
  40. 是否生效保存
  41. Reload privilege tables now? [Y/n] y

注意事项

  1. [fei@centos7 ~]$mysql -uroot -p 普通用户用root登录
  2. Enter password:
  3. MariaDB [(none)]> system cat /etc/passwd //能打开所有文件 并且能修改root密码
  4. root:x:0:0:root:/root:/bin/bash
  5. ···········
  6. 所以 mysqlroot口令千万别泄露
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注