仅限 centos7 以下 版本
- #yum install mysql
- #yum install mysql-server
- #yum install mysql-devel
启动服务
- [root@localhost hadoop]#service mysqld restart
centos 7 mysql-server 失败
- [root@yl-web yl]# yum install mysql-server
- Loaded plugins: fastestmirror
- Loading mirror speeds from cached hostfile
- * base: mirrors.sina.cn
- * extras: mirrors.sina.cn
- * updates: mirrors.sina.cn
- No package mysql-server available.
- Error: Nothing to do
查资料发现是 CentOS 7 版本将 MySQL 数据库软件从默认的程序列表中移除,用 mariadb 代替了。
MariaDB 数据库管理系统是 MySQL 的一个分支,主要由开源社区在维护,采用 GPL 授权许可。开发这个分支的原因之一是:甲骨文公司收购了 MySQL 后,有将 MySQL 闭源的潜在风险,因此社区采用分支的方式来避开这个风险。MariaDB 的目的是完全兼容 MySQL,包括 API 和命令行,使之能轻松成为 MySQL 的代替品。
解决:安装 mariadb
- [root@yl-web yl]# yum install mariadb-server mariadb
- mariadb数据库的相关命令是:
- systemctl start mariadb #启动MariaDB
- systemctl stop mariadb #停止MariaDB
- systemctl restart mariadb #重启MariaDB
- systemctl enable mariadb #设置开机启动
- 所以先启动数据库
- [root@yl-web yl]# systemctl start mariadb
连接 mysql
- [root@localhost hadoop]# mysql -u root -p
- Enter password:
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 2
- Server version: 5.1.73 Source distribution
- Copyright (c) 2000, 2013, 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>
设置密码
- mysql> set password for 'root'@'localhost' =password('password');
- Query OK, 0 rows affected (0.00 sec)
- mysql>
- //不需要重启数据库即可生效。
设置编码
- mysql配置文件为/etc/my.cnf
- 最后加上编码配置
- [mysql]
- default-character-set =utf8
- //这里的字符编码必须和/usr/share/mysql/charsets/Index.xml中一致。
远程连接设置
- 把在所有数据库的所有表的所有权限赋值给位于所有IP地址的root用户。
- mysql> grant all privileges on *.* to root@'%'identified by 'password';
- 如果是新用户而不是root,则要先新建用户
- mysql>create user 'username'@'%' identified by 'password';
- 此时就可以进行远程连接了。
配置 mysql 端口号
- [root@test etc]# vi my.cnf
- [mysqld]
- port=3306 // 加上设置的端口号
- datadir=/var/lib/mysql
- socket=/var/lib/mysql/mysql.sock
- user=mysql
- # Disabling symbolic-links is recommended to prevent assorted security risks
- symbolic-links=0
- [mysqld_safe]
- log-error=/var/log/mysqld.log
- pid-file=/var/run/mysqld/mysqld.pid
- "my.cnf" 11L, 261C written
- [root@test etc]#
- 4. 重新启动mysql
- [root@localhost /]# service mysqld restart
查看端口:
- mysql> show global variables like 'port';
- +---------------+-------+
- | Variable_name | Value |
- +---------------+-------+
- | port | 3306 |
- +---------------+-------+
- 1 row in set (0.00 sec)
- mysql>
远程连接可能还会存在防火墙阻断远程连接失败的情况
- 加入对应mysql端口的 允许
- -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
- 结果:
- [root@localhost /]# vi /etc/sysconfig/iptables // 打开防火墙配置
- # Firewall configuration written by system-config-firewall
- # Manual customization of this file is not recommended.
- *filter
- :INPUT ACCEPT [0:0]
- :FORWARD ACCEPT [0:0]
- :OUTPUT ACCEPT [0:0]
- -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
- -A INPUT -p icmp -j ACCEPT
- -A INPUT -i lo -j ACCEPT
- -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
- -A INPUT -j REJECT --reject-with icmp-host-prohibited
- -A FORWARD -j REJECT --reject-with icmp-host-prohibited
- -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
- COMMIT
- 重启防火墙
- [root@localhost /]# service iptables restart
如果连接失败 或者 telnet ip port 失败 则关闭防火墙
- service iptables stop
- // 服务器重启将会失效
- #vi /etc/my.cnf
- [mysqld]
- log-bin=mysql-bin //[必须]启用二进制日志
- server-id=222 //[必须]服务器唯一ID,默认是1,一般取IP最后一段
- #vi /etc/my.cnf
- [mysqld]
- log-bin=mysql-bin //[不是必须]启用二进制日志
- server-id=226 //[必须]服务器唯一ID,默认是1,一般取IP最后一段
- /etc/init.d / mysql restart
- # / usr / local / mysql / bin / mysql - uroot - pmttang mysql > GRANT REPLICATION SLAVE ON * . * to 'mysync'@'%'identified by 'q123456';
- //一般不用root帐号,“%”表示所有客户端都可能连,只要帐号,密码正确,此处可用具体客户端IP代替,如192.168.1 45.226,加强安全。
- mysql>show master status;
- +------------------+----------+--------------+------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
- +------------------+----------+--------------+------------------+
- | mysql-bin.000004 | 308 | | |
- +------------------+----------+--------------+------------------+
- 1 row in set (0.00 sec)
- 注:执行完此步骤后不要再操作主服务器MYSQL,防止主服务器状态值变化
- mysql>create user 'test'@'%' identified by '123456'; // 创建普通用户,可以远程连接
- mysql> grant select on *.* to test@'%'identified by '123456'; //授权所有库,只能查询操作
- mysql> grant all privileges on *.* to test@'%'identified by '123456'; //这是授予所有权限
- mysql>change master to master_host='192.168.145.222',master_user='mysync',master_password='q123456',
- master_log_file='mysql-bin.000004',master_log_pos=308; //注意不要断开,308数字前后无单引号。
- Mysql>start slave; //启动从服务器复制功能
- change master to master_host='192.168.80.131',master_user='mysync',master_password='q123456',master_log_file='mysql-bin.000001',master_log_pos=251;
- mysql> show slave status\G
- *************************** 1. row ***************************
- Slave_IO_State: Waiting for master to send event
- Master_Host: 192.168.2.222 //主服务器地址
- Master_User: mysync //授权帐户名,尽量避免使用root
- Master_Port: 3306 //数据库端口,部分版本没有此行
- Connect_Retry: 60
- Master_Log_File: mysql-bin.000004
- Read_Master_Log_Pos: 600 //#同步读取二进制日志的位置,大于等于Exec_Master_Log_Pos
- Relay_Log_File: ddte-relay-bin.000003
- Relay_Log_Pos: 251
- Relay_Master_Log_File: mysql-bin.000004
- Slave_IO_Running: Yes //此状态必须YES
- Slave_SQL_Running: Yes //此状态必须YES
- ......
- 注:Slave_IO及Slave_SQL进程必须正常运行,即YES状态,否则都是错误的状态(如:其中一个NO均属错误)。
- 以上操作过程,主从服务器配置完成。
- mysql> insert into mysql.user(Host,User,Password) values("localhost","test",password("1234"));
- 这样就创建了一个名为:test 密码为:1234 的用户。
- 注意:此处的"localhost",是指该用户只能在本地登录,不能在另外一台机器上远程登录。
- 如果想远程登录的话,
- 将"localhost"改为"%",表示在任何一台电脑上都可以登录。也可以指定某台机器可以远程登录。
- 授权格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码";
- 2.1 登录MYSQL(有ROOT权限),这里以ROOT身份登录:
- @>mysql -u root -p
- @>密码
- 2.2 首先为用户创建一个数据库(testDB):
- mysql>create database testDB;
- 2.3 授权test用户拥有testDB数据库的所有权限(某个数据库的所有权限):
- mysql>grant all privileges on testDB.* to test@localhost identified by '1234';
- mysql>flush privileges;//刷新系统权限表
- 格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码";
- 2.4 如果想指定部分权限给一用户,可以这样来写:
- mysql>grant select,update on testDB.* to test@localhost identified by '1234';
- mysql>flush privileges; //刷新系统权限表
- 2.5 授权test用户拥有所有数据库的某些权限:
- mysql>grant select,delete,update,create,drop on *.* to test@"%" identified by "1234";
- //test用户对所有数据库都有select,delete,update,create,drop 权限。
- //@"%" 表示对所有非本地主机授权,不包括localhost。
- (localhost地址设为127.0.0.1,如果设为真实的本地地址,不知道是否可以,没有验证。)
- //对localhost授权:
- 加上一句grant all privileges on testDB.* to test@localhost identified by '1234';即可。
- @>mysql -u root -p
- @>密码
- mysql>Delete FROM user Where User='test' and Host='localhost';
- mysql>flush privileges;
- mysql>drop database testDB; //删除用户的数据库
- 删除账户及权限:>drop user 用户名@'%';
- >drop user 用户名@ localhost;
- 5. 列出所有数据库
- mysql>show database;
- 6. 切换数据库
- mysql>use '数据库名';
- 7. 列出所有表
- mysql>show tables;
- 8. 显示数据表结构
- mysql>describe 表名;
- 9. 删除数据库和数据表
- mysql>drop database 数据库名;
- mysql>drop table 数据表名;
- 主服务器Mysql,建立数据库,并在这个库中建表插入一条数据:
- mysql> create database hi_db;
- Query OK, 1 row affected (0.00 sec)
- mysql> use hi_db;
- Database changed
- mysql> create table hi_tb(id int(3),name char(10));
- Query OK, 0 rows affected (0.00 sec)
- mysql> insert into hi_tb values(001,'bobu');
- Query OK, 1 row affected (0.00 sec)
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | hi_db |
- | mysql |
- | test |
- +--------------------+
- 4 rows in set (0.00 sec)
- 从服务器Mysql查询:
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | hi_db | //I'M here,大家看到了吧
- | mysql |
- | test |
- +--------------------+
- 4 rows in set (0.00 sec)
- mysql> use hi_db
- Database changed
- mysql> select * from hi_tb; //查看主服务器上新增的具体数据
- +------+------+
- | id | name |
- +------+------+
- | 1 | bobu |
- +------+------+
- 1 row in set (0.00 sec)
- 编写一shell脚本,用nagios监控slave的两个yes(Slave_IO及Slave_SQL进程),如发现只有一个或零个yes,就表明主从有问题了,发短信警报吧。
来源: https://www.cnblogs.com/NiceCui/p/8213723.html