[root@iZm5e7sz135n16ua2rmbk6Z local]# wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
你能夠下載到你的本地,而後上傳到linux某目錄下mysql
2.安裝軟件源,在其目錄下輸入linux
[root@iZm5e7sz135n16ua2rmbk6Z local]# rpm -Uvh mysql57-community-release-el7-11.noarch.rpm
3.安裝mysql服務端sql
[root@iZm5e7sz135n16ua2rmbk6Z local]# yum install -y mysql-community-server
4.啓動mysql數據庫
[root@iZm5e7sz135n16ua2rmbk6Z local]# service mysqld start Redirecting to /bin/systemctl start mysqld.service
5.查看mysql運行狀態vim
[root@iZm5e7sz135n16ua2rmbk6Z local]# service mysqld status Redirecting to /bin/systemctl status mysqld.service ● mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled) Active: active (running) since Sun 2019-11-24 14:45:59 CST; 20s ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Process: 14787 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS) Process: 14667 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS) Main PID: 14791 (mysqld) CGroup: /system.slice/mysqld.service └─14791 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid Nov 24 14:45:55 iZm5e7sz135n16ua2rmbk6Z systemd[1]: Starting MySQL Server... Nov 24 14:45:59 iZm5e7sz135n16ua2rmbk6Z systemd[1]: Started MySQL Server.
6.初始化密碼服務器
[root@iZm5e7sz135n16ua2rmbk6Z local]# grep 'temporary password' /var/log/mysqld.log 2019-11-24T06:45:56.831037Z 1 [Note] A temporary password is generated for root@localhost: -cTy=VNwI7!H [root@iZm5e7sz135n16ua2rmbk6Z local]#
每一個人都不同,別複製個人。輸入mysql -uroot -p,而後輸入你的密碼進入ide
[root@iZm5e7sz135n16ua2rmbk6Z local]# mysql -uroot -p Enter password: -cTy=VNwI7!H Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.28 Copyright (c) 2000, 2019, 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> alter user 'root'@'localhost' identified by '%liX9-k<Gwa1';//因爲5.7有策略限制,咱們先隨便改一個複雜的密碼。 Query OK, 0 rows affected (0.00 sec)
mysql> SHOW VARIABLES LIKE 'validate_password%';//查看密碼策略 +--------------------------------------+--------+ | Variable_name | Value | +--------------------------------------+--------+ | validate_password_check_user_name | OFF | | validate_password_dictionary_file | | | validate_password_length | 8 | | validate_password_mixed_case_count | 1 | | validate_password_number_count | 1 | | validate_password_policy | MEDIUM | | validate_password_special_char_count | 1 | +--------------------------------------+--------+ 7 rows in set (0.00 sec) mysql>
mysql> set global validate_password_policy=LOW;//修改限制策略 Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=1;//修改密碼長度 Query OK, 0 rows affected (0.00 sec)
mysql> alter user 'root'@'localhost' identified by '123456';//修改一個簡單的密碼(練習方便,實際不建議這樣作) Query OK, 0 rows affected (0.00 sec)
mysql> update mysql.user set host = '%' where user = 'root';//容許遠程訪問DB Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0
mysql> FLUSH PRIVILEGES;//刷新權限
Query OK, 0 rows affected (0.00 sec)
到這裏就能夠了,反正我是按照上面的代碼複製粘貼徹底能夠成功,不知道大家能夠嗎?spa
這裏我給你們搭建一個簡單的主從(從)配置,就是說一個主節點對應一個從節點,那個從節點下還有一個從節點.線程
1.準備三臺服務器,分別安裝一下mysql,按照上面的按照就能夠的.
2.編輯master配置文件
[root@iZm5eitsswnra4j66dzb9nZ ~]# vim /etc/my.cnf
log-bin=/usr/local/mysql/data/binlog/mysql_bin #打開Mysql日誌,日誌格式爲二進制 mysq_bin結尾是固定的別改,前面是路徑 server-id=1 #[必須]服務器惟一ID,默認是1 sync-binlog=1 #每次執行寫入就與硬盤同步 binlog-do-db=xiaocaijishu #須要同步的二進制數據庫名 expire-logs-days=7 #只保留7天的二進制日誌,以防磁盤被日誌佔滿 binlog-ignore-db=information_schema #不備份的數據庫 binlog-ignore-db=performation_schema #不備份的數據庫 binlog-ignore-db=sys #不備份的數據庫
重啓mysql-master,可能會啓動失敗,是因爲mysql的binlog的用戶組不對,再來改一下用戶組,以咱們的/usr/local/mysql/data/binlog爲例
[root@iZbp1hvg9qhaf4c75o2k58Z binlog]# chown -R mysql /usr/local/mysql/data/binlog/
[root@iZbp1hvg9qhaf4c75o2k58Z binlog]# chgrp -R mysql /usr/local/mysql/data/binlog/
3.設置一個同步帳號
mysql> CREATE USER 'xiaocaijishu'@'47.105.%.%' IDENTIFIED BY '123456'; // 設置帳號xiaocaijishu用於47.105等IP地址的mysql同步,你能夠直接寫%全匹配 Query OK, 0 rows affected (0.00 sec) mysql> grant replication slave,replication client on *.* to xiaocaijishu@'47.105.%.%' identified by 'root';//賦予權限 Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> FLUSH PRIVILEGES; //刷新權限 Query OK, 0 rows affected (0.00 sec)
4.設置slave節點配置文件
server-id=2 #[必須]服務器惟一ID,默認是1 relay_log = /usr/local/mysql/data/binlog/mysql_relay_bin # 開啓中繼日誌 read_only = 1 #設置只讀權限 log_bin=/usr/local/mysql/data/binlog/mysql_bin # 開啓binlog,若是沒有下級節點無須開啓。 log_slave_updates = 1 #使得更新的數據寫進二進制日誌中 replicate-rewrite-db = xiaocaijishu -> xiaocai # 從主節點的xiaocaijishu庫寫入到從節點的xiaocai庫 # replicate-wild-do-table 指定須要同步的表。
5.查找主節點日誌
mysql> show master status; +------------------+----------+--------------+--------------------------------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+--------------------------------------------+-------------------+ | mysql_bin.000001 | 1349 | xiaocaijishu | information_schema,performation_schema,sys | | +------------------+----------+--------------+--------------------------------------------+-------------------+ 1 row in set (0.00 sec)
6.建立主從鏈接
首先咱們進入進入mysql客戶端,而後輸入
mysql> change master to master_host='121.41.60.188',master_user='xiaocaijishu',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=1349; Query OK, 0 rows affected, 2 warnings (0.01 sec)
意思是master_host=「咱們的主節點IP」,master_user=「剛纔建立的主節點用戶」,master_password=「帳戶密碼」,master_log_file=「須要從哪一個二進制binlog複製」,master_log_pos=「對應主節點的Position」
如遇報錯請先輸入
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
而後輸入start slave開啓線程
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
輸入SHOW SLAVE STATUS\G檢查線程是否建立成功
若是看到上面的那個Yes,就說明主從搭建完成了,若是下面還有搭建從節點,開啓binlog,和上面一個道理。
若是遇到
Last_Errno: 1051
Last_Error: Error 'Unknown table 'xiaocai.student'' on query. Default database: 'xiaocai'. Query: 'DROP TABLE `student` /* generated by server */'
能夠嘗試下面的解決方式來處理。
mysql> stop slave; Query OK, 0 rows affected (0.01 sec) mysql> set GLOBAL SQL_SLAVE_SKIP_COUNTER=1; Query OK, 0 rows affected (0.00 sec) mysql> start slave; Query OK, 0 rows affected (0.01 sec)