跳至主要内容

python 代码示例

 

加密软件

谷歌搜registration codes python和license keys python



# 不足十位左边加零
while True:
    num = input('Enter a number : ')
    print('The zero-padded number is : ', str(num).rjust(10, '0'))

查找n个自然数的和

  1. # Sum of natural numbers up to num
  2.  
  3. num = 16
  4.  
  5. if num < 0:
  6.    print("Enter a positive number")
  7. else:
  8.    sum = 0
  9.    # use while loop to iterate until zero
  10.    while(num > 0):
  11.        sum += num
  12.        num -= 1
  13.    print("The sum is", sum)
  14.  


使用匿名函数显示整数2的幂

  1. # Display the powers of 2 using anonymous function
  2.  
  3. terms = 10
  4.  
  5. # Uncomment code below to take input from the user
  6. # terms = int(input("How many terms? "))
  7.  
  8. # use anonymous function
  9. result = list(map(lambda x: 2 ** x, range(terms)))
  10.  
  11. print("The total terms are:",terms)
  12. for i in range(terms):
  13.    print("2 raised to power",i,"is",result[i])
  14.  


查找可被另一个数字整除的数字

  1. # Take a list of numbers
  2. my_list = [12, 65, 54, 39, 102, 339, 221,]
  3.  
  4. # use anonymous function to filter
  5. result = list(filter(lambda x: (x % 13 == 0), my_list))
  6.  
  7. # display the result
  8. print("Numbers divisible by 13 are",result)



将十进制转换为二进制,八进制和十六进制
带有前缀的数字0b 被认为是二进制,0o 被认为是八进制和0x 十六进制


  1. # Python program to convert decimal into other number systems
  2. dec = 344
  3.  
  4. print("The decimal value of", dec, "is:")
  5. print(bin(dec), "in binary.")
  6. print(oct(dec), "in octal.")
  7. print(hex(dec), "in hexadecimal.")
  8.  


查找字符的ASCII值
ord()函数将字符转换为整数(ASCII值)
chr()函数从相应的ASCII值中获取字符

  1. # Program to find the ASCII value of the given character
  2.  
  3. c = 'p'
  4. print("The ASCII value of '" + c + "' is", ord(c))
  5.  
  6. >>> chr(65)
  7. 'A'
  8. >>> chr(120)
  9. 'x'
  10. >>> chr(ord('S') + 1)
  11. 'T'




检查素数

质数,又称素数,指在大于1的自然数中,除了1和该数自身外,无法被其他自然数整除的数

  1. # Program to check if a number is prime or not
  2.  
  3. num = 407
  4.  
  5. # To take input from the user
  6. #num = int(input("Enter a number: "))
  7.  
  8. # prime numbers are greater than 1
  9. if num > 1:
  10.    # check for factors
  11.    for i in range(2,num):
  12.        if (num % i) == 0:
  13.            print(num,"is not a prime number")
  14.            print(i,"times",num//i,"is",num)
  15.            break
  16.    else:
  17.        print(num,"is a prime number")
  18.        
  19. # if input number is less than
  20. # or equal to 1, it is not prime
  21. else:
  22.    print(num,"is not a prime number")
  23.  


在一个范围内打印所有质数

  1. # Python program to display all the prime numbers within an interval
  2.  
  3. lower = 900
  4. upper = 1000
  5.  
  6. print("Prime numbers between", lower, "and", upper, "are:")
  7.  
  8. for num in range(lower, upper + 1):
  9.    # all prime numbers are greater than 1
  10.    if num > 1:
  11.        for i in range(2, num):
  12.            if (num % i) == 0:
  13.                break
  14.        else:
  15.            print(num)
  16.  


检查年份是否为leap年

四年一闰、百年不闰,四百年再闰

  1. # Python program to check if year is a leap year or not
  2.  
  3. year = 2000
  4.  
  5. # To get year (integer input) from the user
  6. # year = int(input("Enter a year: "))
  7.  
  8. if (year % 4) == 0:
  9.    if (year % 100) == 0:
  10.        if (year % 400) == 0:
  11.            print("{0} is a leap year".format(year))
  12.        else:
  13.            print("{0} is not a leap year".format(year))
  14.    else:
  15.        print("{0} is a leap year".format(year))
  16. else:
  17.    print("{0} is not a leap year".format(year))
  18.  

try: print('Please enter year to check for leap year') year = int(input()) except ValueError: print('Please input a valid year') exit(1) if year % 400 == 0: print('Leap Year') elif year % 100 == 0: print('Not Leap Year') elif year % 4 == 0: print('Leap Year') else: print('Not Leap Year')


打印斐波那契序列
斐波那契数列是0、1、1、2、3、5、8 ...的整数序列。
前两项为0和1。所有其他项均通过将前两项相加而获得。这意味着第n个项是第(n-1)个和第(n-2)个项的和

  1. # Program to display the Fibonacci sequence up to n-th term
  2.  
  3. nterms = int(input("How many terms? "))
  4.  
  5. # first two terms
  6. n1, n2 = 0, 1
  7. count = 0
  8.  
  9. # check if the number of terms is valid
  10. if nterms <= 0:
  11.    print("Please enter a positive integer")
  12. elif nterms == 1:
  13.    print("Fibonacci sequence upto",nterms,":")
  14.    print(n1)
  15. else:
  16.    print("Fibonacci sequence:")
  17.    while count < nterms:
  18.        print(n1)
  19.        nth = n1 + n2
  20.        # update values
  21.        n1 = n2
  22.        n2 = nth
  23.        count += 1
  24.  



检查阿姆斯特朗号(3位数字)
153 = 1 * 1 * 1 + 5 * 5 * 5 + 3 * 3 * 3 // 153是阿姆斯特朗数

  1. # Python program to check if the number is an Armstrong number or not
  2.  
  3. # take input from the user
  4. num = int(input("Enter a number: "))
  5.  
  6. # initialize sum
  7. sum = 0
  8.  
  9. # find the sum of the cube of each digit
  10. temp = num
  11. while temp > 0:
  12.    digit = temp % 10
  13.    sum += digit ** 3
  14.    temp //= 10
  15.  
  16. # display the result
  17. if num == sum:
  18.    print(num,"is an Armstrong number")
  19. else:
  20.    print(num,"is not an Armstrong number")
  21.  



检查阿姆斯壮的n位数字

  1. num = 1634
  2.  
  3. # Changed num variable to string,
  4. # and calculated the length (number of digits)
  5. order = len(str(num))
  6.  
  7. # initialize sum
  8. sum = 0
  9.  
  10. # find the sum of the cube of each digit
  11. temp = num
  12. while temp > 0:
  13.    digit = temp % 10
  14.    sum += digit ** order
  15.    temp //= 10
  16.  
  17. # display the result
  18. if num == sum:
  19.    print(num,"is an Armstrong number")
  20. else:
  21.    print(num,"is not an Armstrong number")
  22.  



两个整数之间的所有Armstrong数字

  1. # Program to check Armstrong numbers in a certain interval
  2.  
  3. lower = 100
  4. upper = 2000
  5.  
  6. for num in range(lower, upper + 1):
  7.  
  8.    # order of number
  9.    order = len(str(num))
  10.    
  11.    # initialize sum
  12.    sum = 0
  13.  
  14.    temp = num
  15.    while temp > 0:
  16.        digit = temp % 10
  17.        sum += digit ** order
  18.        temp //= 10
  19.  
  20.    if num == sum:
  21.        print(num)
  22.  


生成一个十个字符的字母数字密码,至少包含一个小写字符,至少一个大写字符和至少三位数字

https://docs.python.org/3/library/secrets.html

  1. import string
  2. import secrets
  3. alphabet = string.ascii_letters + string.digits
  4. while True:
  5.     password = ''.join(secrets.choice(alphabet) for i in range(10))
  6.     if (any(c.islower() for c in password)
  7.             and any(c.isupper() for c in password)
  8.             and sum(c.isdigit() for c in password) >= 3):
  9.         break
  10. print(password)





分别使用字典、使用列表和字典理解来计算字符串中每个元音的数量

  1. # Program to count the number of each vowels
  2.  
  3. # string of vowels
  4. vowels = 'aeiou'
  5.  
  6. ip_str = 'Hello, have you tried our tutorial section yet?'
  7.  
  8. # make it suitable for caseless comparisions
  9. # 方法casefold()返回字符串的小写版本
  10. ip_str = ip_str.casefold()
  11.  
  12. # make a dictionary with each vowel a key and value 0
  13. count = {}.fromkeys(vowels,0)
  14.  
  15. # count the vowels
  16. for char in ip_str:
  17.    if char in count:
  18.        count[char] += 1
  19.  
  20. print(count)
  21.  
  22. ---------------------------------------------------------------------
  23.  
  24. # Using dictionary and list comprehension
  25.  
  26. ip_str = 'Hello, have you tried our tutorial section yet?'
  27.  
  28. # make it suitable for caseless comparisions
  29. ip_str = ip_str.casefold()
  30.  
  31. # count the vowels
  32. count = {x:sum([1 for char in ip_str if char == x]) for x in 'aeiou'}
  33.  
  34. print(count)
  35.  



按字母顺序对单词进行排序

  1. # Program to sort alphabetically the words form a string provided by the user
  2.  
  3. my_str = "Hello this Is an Example With cased letters"
  4.  
  5. # To take input from the user
  6. #my_str = input("Enter a string: ")
  7.  
  8. # breakdown the string into a list of words
  9. words = [word.lower() for word in my_str.split()]
  10.  
  11. # sort the list
  12. words.sort()
  13.  
  14. # display the sorted words
  15. print("The sorted words are:")
  16. for word in words:
  17.    print(word)
  18.  


从字符串中删除标点符号

  1. # define punctuation
  2. punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
  3.  
  4. my_str = "Hello!!!, he said ---and went."
  5.  
  6. # To take input from the user
  7. # my_str = input("Enter a string: ")
  8.  
  9. # remove punctuation from the string
  10. no_punct = ""
  11. for char in my_str:
  12.    if char not in punctuations:
  13.        no_punct = no_punct + char
  14.  
  15. # display the unpunctuated string
  16. print(no_punct)
  17.  


检查字符串是否为回文

  1. # Program to check if a string is palindrome or not
  2.  
  3. my_str = 'aIbohPhoBiA'
  4.  
  5. # make it suitable for caseless comparison
  6. my_str = my_str.casefold()
  7.  
  8. # reverse the string
  9. rev_str = reversed(my_str)
  10.  
  11. # check if the string is equal to its reverse
  12. if list(my_str) == list(rev_str):
  13.    print("The string is a palindrome.")
  14. else:
  15.    print("The string is not a palindrome.")
  16.  


查找数字阶乘

  1. # Python program to find the factorial of a number provided by the user.
  2.  
  3. # change the value for a different result
  4. num = 7
  5.  
  6. # To take input from the user
  7. #num = int(input("Enter a number: "))
  8.  
  9. factorial = 1
  10.  
  11. # check if the number is negative, positive or zero
  12. if num < 0:
  13.    print("Sorry, factorial does not exist for negative numbers")
  14. elif num == 0:
  15.    print("The factorial of 0 is 1")
  16. else:
  17.    for i in range(1,num + 1):
  18.        factorial = factorial*i
  19.    print("The factorial of",num,"is",factorial)
  20.  


使用递归查找数字的阶乘

数字的阶乘是从1到该数字的所有整数的乘积。
例如,阶乘6是1*2*3*4*5*6 = 720。没有为负数定义的阶乘,零阶的阶乘是1、0!= 1

  1. # Factorial of a number using recursion
  2.  
  3. def recur_factorial(n):
  4.    if n == 1:
  5.        return n
  6.    else:
  7.        return n*recur_factorial(n-1)
  8.  
  9. num = 7
  10.  
  11. # check if the number is negative
  12. if num < 0:
  13.    print("Sorry, factorial does not exist for negative numbers")
  14. elif num == 0:
  15.    print("The factorial of 0 is 1")
  16. else:
  17.    print("The factorial of", num, "is", recur_factorial(num))
  18.  



使用递归将十进制转换为二进制


  1. # Function to print binary number using recursion
  2. def convertToBinary(n):
  3.    if n > 1:
  4.        convertToBinary(n//2)
  5.    print(n % 2,end = '')
  6.  
  7. # decimal number
  8. dec = 34
  9.  
  10. convertToBinary(dec)
  11. print()
  12.  



使用递归计算给定数字的总和

  1. # Python program to find the sum of natural using recursive function
  2.  
  3. def recur_sum(n):
  4.    if n <= 1:
  5.        return n
  6.    else:
  7.        return n + recur_sum(n-1)
  8.  
  9. # change this value for a different result
  10. num = 16
  11.  
  12. if num < 0:
  13.    print("Enter a positive number")
  14. else:
  15.    print("The sum is",recur_sum(num))
  16.  



使用递归显示斐波那契数列
斐波那契数列是0、1、1、2、3、5、8 ...的整数序列。
前两个项是0和1。所有其他项是通过将前两个项相加而获得的。这意味着第n个项是第(n-1)个和第(n-2)个项的和
  1. # Python program to display the Fibonacci sequence
  2.  
  3. def recur_fibo(n):
  4.    if n <= 1:
  5.        return n
  6.    else:
  7.        return(recur_fibo(n-1) + recur_fibo(n-2))
  8.  
  9. nterms = 10
  10.  
  11. # check if the number of terms is valid
  12. if nterms <= 0:
  13.    print("Plese enter a positive integer")
  14. else:
  15.    print("Fibonacci sequence:")
  16.    for i in range(nterms):
  17.        print(recur_fibo(i))
  18.  




制作一个简单的计算器

  1. # Program make a simple calculator
  2.  
  3. # This function adds two numbers
  4. def add(x, y):
  5.     return x + y
  6.  
  7. # This function subtracts two numbers
  8. def subtract(x, y):
  9.     return x - y
  10.  
  11. # This function multiplies two numbers
  12. def multiply(x, y):
  13.     return x * y
  14.  
  15. # This function divides two numbers
  16. def divide(x, y):
  17.     return x / y
  18.  
  19.  
  20. print("Select operation.")
  21. print("1.Add")
  22. print("2.Subtract")
  23. print("3.Multiply")
  24. print("4.Divide")
  25.  
  26. while True:
  27.     # Take input from the user
  28.     choice = input("Enter choice(1/2/3/4): ")
  29.  
  30.     # Check if choice is one of the four options
  31.     if choice in ('1', '2', '3', '4'):
  32.         num1 = float(input("Enter first number: "))
  33.         num2 = float(input("Enter second number: "))
  34.  
  35.         if choice == '1':
  36.             print(num1, "+", num2, "=", add(num1, num2))
  37.  
  38.         elif choice == '2':
  39.             print(num1, "-", num2, "=", subtract(num1, num2))
  40.  
  41.         elif choice == '3':
  42.             print(num1, "*", num2, "=", multiply(num1, num2))
  43.  
  44.         elif choice == '4':
  45.             print(num1, "/", num2, "=", divide(num1, num2))
  46.         break
  47.     else:
  48.         print("Invalid Input")
  49.  



随机播放一副纸牌

  1. # Python program to shuffle a deck of card
  2.  
  3. # importing modules
  4. import itertools, random
  5.  
  6. # make a deck of cards
  7. deck = list(itertools.product(range(1,14),['Spade','Heart','Diamond','Club']))
  8.  
  9. # shuffle the cards
  10. random.shuffle(deck)
  11.  
  12. # draw five cards
  13. print("You got:")
  14. for i in range(5):
  15.    print(deck[i][0], "of", deck[i][1])




显示给定日期的日历

  1. # Program to display calendar of the given month and year
  2.  
  3. # importing calendar module
  4. import calendar
  5.  
  6. yy = 2014  # year
  7. mm = 11    # month
  8.  
  9. # To take month and year input from the user
  10. # yy = int(input("Enter year: "))
  11. # mm = int(input("Enter month: "))
  12.  
  13. # display the calendar
  14. print(calendar.month(yy, mm))
  15.  


查找两个数字的最小公倍数(LCM:The least common multiple 

  1. # Python Program to find the L.C.M. of two input number
  2.  
  3. def compute_lcm(x, y):
  4.  
  5.    # choose the greater number
  6.    if x > y:
  7.        greater = x
  8.    else:
  9.        greater = y
  10.  
  11.    while(True):
  12.        if((greater % x == 0) and (greater % y == 0)):
  13.            lcm = greater
  14.            break
  15.        greater += 1
  16.  
  17.    return lcm
  18.  
  19. num1 = 54
  20. num2 = 24
  21.  
  22. print("The L.C.M. is", compute_lcm(num1, num2))
  23.  

两个数的乘积等于这两个数的最小公倍数和最大公除数的乘积
Number1 * Number2 = L.C.M. * G.C.D.
使用欧几里得算法计算两个数字的最大公除数(GCD:greatest common divisor)


  1. # Python program to find the L.C.M. of two input number
  2.  
  3. # This function computes GCD
  4. def compute_gcd(x, y):
  5.  
  6.    while(y):
  7.        x, y = y, x % y
  8.    return x
  9.  
  10. # This function computes LCM
  11. def compute_lcm(x, y):
  12.    lcm = (x*y)//compute_gcd(x,y)
  13.    return lcm
  14.  
  15. num1 = 54
  16. num2 = 24
  17.  
  18. print("The L.C.M. is", compute_lcm(num1, num2))
  19.  


查找HCF或GCD
两个数字的最高公共因子(HCF)或最大公共除数(GCD)是将两个给定数字完美除的最大正整数
  1. # Python program to find H.C.F of two numbers
  2.  
  3. # define a function
  4. def compute_hcf(x, y):
  5.  
  6. # choose the smaller number
  7.     if x > y:
  8.         smaller = y
  9.     else:
  10.         smaller = x
  11.     for i in range(1, smaller+1):
  12.         if((x % i == 0) and (y % i == 0)):
  13.             hcf = i
  14.     return hcf
  15.  
  16. num1 = 54
  17. num2 = 24
  18.  
  19. print("The H.C.F. is", compute_hcf(num1, num2))
  20.  
  21. # 使用欧几里得算法
  22. # Function to find HCF the Using Euclidian algorithm
  23. def compute_hcf(x, y):
  24.    while(y):
  25.        x, y = y, x % y
  26.    return x
  27.  
  28. hcf = compute_hcf(300, 400)
  29. print("The HCF is", hcf)
  30.  

用于查找文件哈希的Python程序

  1. # Python rogram to find the SHA-1 message digest of a file
  2.  
  3. # importing the hashlib module
  4. import hashlib
  5.  
  6. def hash_file(filename):
  7.    """"This function returns the SHA-1 hash
  8.   of the file passed into it"""
  9.  
  10.    # make a hash object
  11.    h = hashlib.sha1()
  12.  
  13.    # open file for reading in binary mode
  14.    with open(filename,'rb') as file:
  15.  
  16.        # loop till the end of the file
  17.        chunk = 0
  18.        while chunk != b'':
  19.            # read only 1024 bytes at a time
  20.            chunk = file.read(1024)
  21.            h.update(chunk)
  22.  
  23.    # return the hex representation of digest
  24.    return h.hexdigest()
  25.  
  26. message = hash_file("track1.mp3")
  27. print(message)
  28.  


Python 之 生成数字签名

  1. import base64
  2.  
  3. from Crypto.Hash import SHA256
  4. from Crypto.PublicKey import RSA
  5. from Crypto.Signature import PKCS1_v1_5
  6.  
  7. private_key = b"""-----BEGIN RSA PRIVATE KEY-----
  8. MIICXgIBAAKBgQDjzE4mbSVTGxstkMT4zREzNX0wZCLzzNW4vDA9QH0pFMBGrgLF
  9. KPAmcTXa7wYLybUOKuQ/og9JCId9AgQuKFTyh67654nhugOPf0m8kcvRr77mAP56
  10. Po6fyrr8GWZBpbQ7hc+1EMNPc/E1REe3EMEzFaDFaQgCMwcwUTMiGGZ20QIDAQAB
  11. AoGAaij2arYG5PoG6m9DPGflEiZlVz3zhAb7uwIIwSLisVh4WvgRVmzDrkaoQIWQ
  12. HcI83INkp7sQwIp3Cez8ob4uB4/ZWdYvpSFY3dco7/iq+kQgycOwz34DVmUWddlc
  13. bXwoRanw/aH48AycQHO3QuBQeMkfeW33TbmpbzeDnF1unYECQQDzQmaxA28bn8Ec
  14. DnD/WbFOxIw/aIRKB4jEVgDfKOhGveTTtXor5UsxnRQYlFaONon9/coP/Ca4y27a
  15. Acl7Bw4ZAkEA77qZro7vme5D5tIo8PxWEGKwfPMw7F+f4Y6l9+LeJvCOgChZR1mM
  16. hODemhe8pbeyFMSSZPWQZq1XAexMDMbVeQJBALJgmZ7pzrqp7dgd+tw0MCF7XQBO
  17. KtuCJNcb3a3GeHUPYFGiPMEddYMfFRJlDAdilNOsG1SXaOmPO20fbFgLt1kCQQCq
  18. iDcisZNIEPJElGODaj1e0pVxjR3USAHX1j3CJKSbVqxIBmvcEZugOsafHxuXVyFb
  19. HKp3HyhlohEu0QUYYakhAkEA7P5Fdw0JryJC2S7QodiTIXOfhn6WEtuVCX+Nz8Lc
  20. ue+ojBfrIMWHzfGWBc+JjFHbjV/XtdLpebnr5Pay1oZtRA==
  21. -----END RSA PRIVATE KEY-----
  22. """
  23.  
  24. def sign(key, data):
  25.     RSA.import_key
  26.     private_key = RSA.importKey(key)
  27.     cipher = PKCS1_v1_5.new(private_key)
  28.     h = SHA256.new(data)
  29.     signature = cipher.sign(h)
  30.     return base64.b64encode(signature)
  31.  
  32. ts = "1595319666236"
  33. baseUrl = "https://api-gateway.walmart.com/v3/feeds"
  34. consumerId = "123456"
  35.  
  36. def main():
  37.     httpMethod = "GET"
  38.     timestamp = ts # 测试需要
  39.     stringToSign = consumerId + "\n" + baseUrl + "\n" + httpMethod + "\n" + timestamp + "\n"
  40.     r = sign(private_key,stringToSign.encode())
  41.     print(r)
  42. if __name__ == "__main__":
  43.     main()







评论

此博客中的热门博文

自动发送消息

  # https://pyperclip.readthedocs.io/en/latest/ import pyperclip while True :     # pyperclip.copy('Hello, world!')     # pyperclip.paste()     # pyperclip.waitForPaste()     print ( pyperclip. waitForNewPaste ( ) )     # 获取要输入新的坐标,也可以通过autohotkey import time import pyautogui  as pag import os   try :     while True :         print ( "Press Ctrl-C to end" )         x , y = pag. position ( )   # 返回鼠标的坐标         posStr = "Position:" + str ( x ) . rjust ( 4 ) + ',' + str ( y ) . rjust ( 4 )         print ( posStr )   # 打印坐标         time . sleep ( 0.2 )         os . system ( 'cls' )   # 清楚屏幕 except KeyboardInterrupt :     print ( 'end....' )     # 打印消息 import pyautogui import time import pyperclip   content = """   呼叫龙叔! 第二遍! 第三遍! 第四遍...

学习地址

清华大学计算机系课程攻略 https://github.com/PKUanonym/REKCARC-TSC-UHT 浙江大学课程攻略共享计划 https://github.com/QSCTech/zju-icicles https://home.unicode.org/ 世界上的每个人都应该能够在手机和电脑上使用自己的语言。 http://codecanyon.net   初次看到这个网站,小伙伴们表示都惊呆了。原来代码也可以放在网上卖的?!! 很多coder上传了各种代码,每个代码都明码标价。看了下销售排行,有的19刀的卖了3万多份,额di神啊。可以看到代码的演示效果,真的很漂亮。代码以php、wordpress主题、Javascript、css为主,偏前台。 https://www.lintcode.com/ 算法学习网站,上去每天刷两道算法题,走遍天下都不怕。 https://www.codecademy.com/ 包含在线编程练习和课程视频 https://www.reddit.com/ 包含有趣的编程挑战题,即使不会写,也可以查看他人的解决方法。 https://ideone.com/ 在线编译器,可运行,可查看代码示例。 http://it-ebooks.info/ 大型电子图书馆,可即时免费下载书籍。 刷题 https://github.com/jackfrued/Python-100-Days https://github.com/kenwoodjw/python_interview_question 面试问题 https://github.com/kenwoodjw/python_interview_question https://www.journaldev.com/15490/python-interview-questions#python-interpreter HTTP 身份验证 https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Authentication RESTful 架构详解 https://www.runoob.com/w3cnote/restful-architecture.html https://www.rosettacode.org/wiki/Rosetta_C...

mysql 入门

资料 https://dinfratechsource.com/2018/11/10/how-to-install-latest-mysql-5-7-21-on-rhel-centos-7/ https://dev.mysql.com/doc/refman/5.7/en/linux-installation-yum-repo.html https://www.runoob.com/mysql/mysql-create-database.html https://www.liquidweb.com/kb/install-java-8-on-centos-7/ 工具 https://www.heidisql.com/ HeidiSQL是免费软件,其目标是易于学习。 “ Heidi”使您可以从运行数据库系统MariaDB,MySQL,Microsoft SQL或PostgreSQL的计算机上查看和编辑数据和结构 MySQL 连接时尽量使用 127.0.0.1 而不是 localhost localhost 使用的 Linux socket,127.0.0.1 使用的是 tcp/ip 为什么我使用 localhost 一直没出问题 因为你的本机中只有一个 mysql 进程, 如果你有一个 node1 运行在 3306, 有一个 node2 运行在 3307 mysql -u root -h localhost -P 3306 mysql -u root -h localhost -P 3307 都会连接到同一个 mysql 进程, 因为 localhost 使用 Linux socket, 所以 -P 字段直接被忽略了, 等价于 mysql -u root -h localhost mysql -u root -h localhost 而 -h 默认是 localhost, 又等价于 mysql -u root mysql -u root 为了避免这种情况(比如你在本地开发只有一个 mysql 进程,线上或者 qa 环境有多个 mysql 进程)最好的方式就是使用 IP mysql -u root -h 127 .0 .0 .1 -P 3307 strac...